页面刷新后 DropDownList SelectedIndex 在 FireFox 中不起作用
我在 UpdatePanel 中有 DropDownList ,如下所示:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<div>
Index: <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>
</ContentTemplate>
</asp:UpdatePanel>
在我的代码隐藏中,我有这个简单的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillDropDownList();
}
}
private void FillDropDownList()
{
for (int i = 0; i < 10; i++)
{
DropDownList1.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
DropDownList1.SelectedIndex = 0;
Label1.Text = DropDownList1.SelectedIndex.ToString();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = DropDownList1.SelectedIndex.ToString();
}
问题是:我在列表中选择一些大于 0 的项目(例如 5),标签显示值 5。但是当我刷新页面,通过点击 Firefox 中的刷新按钮,标签显示值 0(正如它应该的那样),但下拉列表显示 5。我检查了页面 html 源,下拉列表选择了值 0,但显示 5。但是,当我通过将光标放在地址栏中并按 Enter 刷新页面时,一切正常(下拉列表显示 0)。该问题仅出现在 FireFox 中(我的版本是 3.5.7)。
有什么想法会导致这个问题吗?
I've got DropDownList in UpdatePanel as shown below:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<div>
Index: <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>
</ContentTemplate>
</asp:UpdatePanel>
In my codebehind i've got this simple code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillDropDownList();
}
}
private void FillDropDownList()
{
for (int i = 0; i < 10; i++)
{
DropDownList1.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
DropDownList1.SelectedIndex = 0;
Label1.Text = DropDownList1.SelectedIndex.ToString();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = DropDownList1.SelectedIndex.ToString();
}
Here is the problem: I select in the list some item greater than 0 (e.g. 5), the label shows value 5. But when I refresh the page, by hitting the refresh button in firefox, the label shows value 0 (as it's supposed to) but the dropdownlist shows 5. I checked the page html source and the dropdown has selected value 0, but shows 5. However, When I refresh the page by putting cursor in address bar and press enter everythig works fine (drowdownlist shows 0). The problem occurs only in FireFox (I've got version 3.5.7).
Any ideas what can cause this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Firefox 会记住会话中每个选择的 selectedIndex。这对用户来说有好处,但对开发人员来说却很麻烦......我也遇到了同样的问题。如果我找到解决方案,我会发布它。
看看这个:
https://developer.mozilla.org/en/Using_Firefox_1.5_caching
它有效!
在 PHP 中:
Firefox remembers the selectedIndex of each select in a session. It's good for a user but it's a hassle for developers... I'm having the same problem. If I find a solution I'll post it.
Check this out:
https://developer.mozilla.org/en/Using_Firefox_1.5_caching
It works!
In PHP:
您可以在表单中添加一个名为
autocomplete
的属性,并将其设置为off
以防止 Firefox 中出现此行为。我发现这是解决这个问题的最简单的方法。例如。
如果您担心这不是有效的 (X)HTML,那么您可以使用 jQuery 执行相同的操作:
You can add an attribute to your forms called
autocomplete
and set it tooff
to prevent this behaviour in Firefox. I've found this the simplest method of solving this problem.eg.
If you are worried about this not being valid (X)HTML then you can do the same thing using jQuery:
对于遇到此“后退缓存”问题的任何人,此博文确实让我明白了这个问题。
For anyone who comes across this "Back-Forward-Cache" problem, this blogpost really enlightened the problem for me.