DropdownList reset to to index 0 on load

江枫思渺然 提交于 2019-12-23 13:58:30

问题


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

  • I am not using a form
  • 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 then please explain.


回答1:


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">

<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).




回答2:


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;
}



回答3:


In your script under code HTML:

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

Happy Coding ^^




回答4:


stop Firefox from retaining the viewstate and repopulating the form:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Browser.Browser == "Firefox")
            Form.Attributes.Add("autocomplete", "off");
    }


来源:https://stackoverflow.com/questions/674315/dropdownlist-reset-to-to-index-0-on-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!