Dropdownlist selectedindex在页面刷新时不起作用

时间:2022-11-19 22:11:37

I'm having a problem with a very normal ASP.NET dropdownlist. I have a dropdownlist in the page with AutoPostback=false. And a code to generate dropdownlist’s content:

我遇到了一个非常普通的ASP.NET下拉列表问题。我在页面中有一个下拉列表,其中AutoPostback = false。以及生成下拉列表内容的代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        this.dropDownList.Items.Add(new ListItem("text1", "value1"));
        this.dropDownList.Items.Add(new ListItem("text2", "value2"));
        this.dropDownList.SelectedIndex = 0;
    }
}
  • Run the page, and choose the second option: text2.
  • 运行该页面,然后选择第二个选项:text2。
  • After that, press F5, this will do a page load (note that I haven't done any postback yet, just change the selected item of Dropdownlist and press F5)
  • 之后,按F5,这将进行页面加载(请注意我还没有做任何回发,只需更改Dropdownlist的选定项目并按F5)

I expected that the dropdownlist will now selected the default item with index = 0, but the selected item is still the item “text2”- the item that I have chosen. In this situation, the this.dropDownList.SelectedIndex = 0; doesn’t work.

我预计下拉列表现在将选择index = 0的默认项目,但所选项目仍然是项目“text2” - 我选择的项目。在这种情况下,this.dropDownList.SelectedIndex = 0;不起作用。

I totally don’t understand. Could anyone help me?

我完全不明白。谁能帮助我?

Update: This behavior only happens in Firefox, it doesn't happen in Chrome/IE.

更新:此行为仅在Firefox中发生,在Chrome / IE中不会发生。

2 个解决方案

#1


2  

It seems like you're misunderstanding what post back means; it does not mean just a page refresh. When you hit F5 in the browser, the original request will be sent to the server, and Page.IsPostBack will be false.

你似乎误解了回发的含义;它并不仅仅意味着页面刷新。当您在浏览器中点击F5时,原始请求将被发送到服务器,而Page.IsPostBack将为false。

If you want to do an actual postback, add an asp:Button server control to the page and click it (or just set AutoPostBack=true on the DropDownList).

如果你想做一个实际的回发,添加一个asp:Button服务器控件到页面并单击它(或只是在DropDownList上设置AutoPostBack = true)。


It seems like Firefox is doing some kind of client-side caching, so it doesn't make another round-trip to the server when you hit F5. You can work around this by resetting the select element when the page loads.

似乎Firefox正在进行某种客户端缓存,因此当你点击F5时它不会再次进入服务器。您可以通过在页面加载时重置select元素来解决此问题。

<script type='text/javascript'>
    document.getElementById('<%= dropDownList.ClientID %>').selectedIndex = 0;
</script>

(Note: put this at the bottom of the page so that it executes after the document has loaded.)

(注意:将它放在页面底部,以便在文档加载后执行。)

#2


1  

When you press F5, it will send the post headers along with the request.
Your last request was to select the second option. So that gets selected.

当您按F5时,它将发送帖子标题和请求。您的上一个请求是选择第二个选项。这样就被选中了。

If you type the url again, or hit enter in the address bar; you will not send the post data. In this case your SelectedIndex will be 0.

如果您再次输入网址,或在地址栏中按Enter键;你不会发送帖子数据。在这种情况下,您的SelectedIndex将为0。

#1


2  

It seems like you're misunderstanding what post back means; it does not mean just a page refresh. When you hit F5 in the browser, the original request will be sent to the server, and Page.IsPostBack will be false.

你似乎误解了回发的含义;它并不仅仅意味着页面刷新。当您在浏览器中点击F5时,原始请求将被发送到服务器,而Page.IsPostBack将为false。

If you want to do an actual postback, add an asp:Button server control to the page and click it (or just set AutoPostBack=true on the DropDownList).

如果你想做一个实际的回发,添加一个asp:Button服务器控件到页面并单击它(或只是在DropDownList上设置AutoPostBack = true)。


It seems like Firefox is doing some kind of client-side caching, so it doesn't make another round-trip to the server when you hit F5. You can work around this by resetting the select element when the page loads.

似乎Firefox正在进行某种客户端缓存,因此当你点击F5时它不会再次进入服务器。您可以通过在页面加载时重置select元素来解决此问题。

<script type='text/javascript'>
    document.getElementById('<%= dropDownList.ClientID %>').selectedIndex = 0;
</script>

(Note: put this at the bottom of the page so that it executes after the document has loaded.)

(注意:将它放在页面底部,以便在文档加载后执行。)

#2


1  

When you press F5, it will send the post headers along with the request.
Your last request was to select the second option. So that gets selected.

当您按F5时,它将发送帖子标题和请求。您的上一个请求是选择第二个选项。这样就被选中了。

If you type the url again, or hit enter in the address bar; you will not send the post data. In this case your SelectedIndex will be 0.

如果您再次输入网址,或在地址栏中按Enter键;你不会发送帖子数据。在这种情况下,您的SelectedIndex将为0。