views:

2140

answers:

3

Hi,

How would I reset my asp:DropDownList element (which has a runat="server") to index 0 every time the page is "reloaded" in firefox (F5 is pressed).

If you suggest using javascript...please note that A: I am not using a form and B: I don't know how to access elements that have a runat="server" with javascript

If this can be done using script on the .aspx page....please explain things you think an ASP newb would not know (ie. me, lol)

thanks, Andrew :)

+1  A: 

put code in the Page_Load event to do this

protected void Page_Load(object sender, EventArgs e)
{    
    myDropDownList.SelectedIndex =0;
}

EDIT:

In response to your comments, If you have put the above logic inside of an if statement to check whether Page.IsPostback = false, then the selected index will not be set back to 0 upon refresh (which performs a client postback). As an example to demonstrate this, here is a page with a dropdown list set to autopostback upon selection

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>My Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="ddl" runat="server" AutoPostBack="true" >
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>

Here is the code behind

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Init(object sender, EventArgs e)
    {
        //Apologies for Dairy Produce inspired list
        ddl.Items.Add(new ListItem("Cheese"));
        ddl.Items.Add(new ListItem("Yoghurt"));
        ddl.Items.Add(new ListItem("Milk"));
        ddl.Items.Add(new ListItem("Butter"));
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //Run the Page with this in first, then comment out
        //the if statement to leave only ddl.SelectedIndex = 0;

        if (!Page.IsPostBack)
        {
            ddl.SelectedIndex = 0;
        }
    }
}

As will be demonstrated, when the page is originally ran, upon refresh, the selected index will be retained within the dropdown list; When the if statement is commented out however, upon refresh, the selected index is set to 0 (which in this case is Cheese).

Russ Cam
This is the first thing I tried. It doesn't work...the DropdownList seems to be reset to it's previous selection by the cache.Not that is should make a difference but I put this command int he "If Page.IsPostBack = False" statement
Andrew
also..i assume you mean from the code behind?
Andrew
It could either be in a code-behind file (preferred approach) or within script tags in aspx markup (akin to Classic ASP). The likely source of a previous index would be Viewstate, but that gets set in Pre_Load and would be overridden by the above statement.
Russ Cam
If you put this inside if Page.IsPostback = false, then the statement will only run when the page is first loaded, not in response to a client postback, which is what a refresh would be
Russ Cam
A: 

Just add this code to your Page_Load event :

if (myDropDown.Items.Count > 0)
{
    myDropDown.Items[myDropDown.SelectedIndex].Selected = false;
    myDropDown.Items[0].Selected = true;
}
Canavar
Already tried this. doesn't work. if you check the SelectedIndex of dropdownlist in the Page_load it returns 0....but a different index is visible...i think the browser cache resets it after the code executes (i am using firefox) ..i assume you mean from the code behind?
Andrew
A: 

In your script under code HTML:

B01 = document.getElementById('<%=me.yourID.clientid %>');
B01.selectedIndex = 0;

Happy Coding ^^

parrkiid