DropdownList 在加载时重置为索引 0

发布于 2024-07-15 19:22:07 字数 311 浏览 6 评论 0原文

每次在 Firefox 中“重新加载”页面(按 F5)时,如何将我的 asp:DropDownList 元素(具有 runat="server")重置为索引 0 )?

如果您建议使用 JavaScript,请注意,

  • 我没有使用表单,
  • 我不知道如何使用 JavaScript 访问具有 runat="server" 的元素,

如果可以使用 JavaScript 上的脚本来完成此操作.aspx 页面请解释一下。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我三岁 2024-07-22 19:22:07

将代码放入 Page_Load 事件中以执行此操作

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

编辑:< /strong>

针对您的评论,如果您将上述逻辑放在 if 语句中以检查是否 Page.IsPostback = false,则刷新时所选索引将不会设置回 0 (执行客户端回发)。 作为演示这一点的示例,这是一个下拉列表设置为选择后自动回发的页面

<%@ 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>

这是后面的代码

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

正如将要演示的,当页面最初运行时,刷新后,所选索引将保留在下拉列表中; 然而,当 if 语句被注释掉时,刷新后,所选索引将设置为 0(在本例中为 Cheese)。

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

可可 2024-07-22 19:22:07

只需将此代码添加到您的 Page_Load 事件中:

if (myDropDown.Items.Count > 0)
{
    myDropDown.Items[myDropDown.SelectedIndex].Selected = false;
    myDropDown.Items[0].Selected = true;
}

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;
}
流星番茄 2024-07-22 19:22:07

在您的脚本中,在 HTML 代码下:

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

Happy Coding ^^

In your script under code HTML:

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

Happy Coding ^^

心是晴朗的。 2024-07-22 19:22:07

阻止 Firefox 保留视图状态并重新填充表单:

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

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");
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文