页面刷新后 DropDownList SelectedIndex 在 FireFox 中不起作用

发布于 2024-08-27 10:56:18 字数 1352 浏览 3 评论 0原文

我在 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 技术交流群。

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

发布评论

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

评论(3

愁以何悠 2024-09-03 10:56:18

Firefox 会记住会话中每个选择的 selectedIndex。这对用户来说有好处,但对开发人员来说却很麻烦......我也遇到了同样的问题。如果我找到解决方案,我会发布它。

看看这个:
https://developer.mozilla.org/en/Using_Firefox_1.5_caching

它有效!

在 PHP 中:

<?
    header("cache-control: no-store");
    header("Pragma: no-cache");
?>

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:

<?
    header("cache-control: no-store");
    header("Pragma: no-cache");
?>
洋洋洒洒 2024-09-03 10:56:18

您可以在表单中添加一个名为 autocomplete 的属性,并将其设置为 off 以防止 Firefox 中出现此行为。我发现这是解决这个问题的最简单的方法。

例如。

<form id="myForm" action="/submithandler/" method="get" autocomplete="off">
...
</form>

如果您担心这不是有效的 (X)HTML,那么您可以使用 jQuery 执行相同的操作:

$("#myForm").attr("autocomplete", "off");

You can add an attribute to your forms called autocomplete and set it to off to prevent this behaviour in Firefox. I've found this the simplest method of solving this problem.

eg.

<form id="myForm" action="/submithandler/" method="get" autocomplete="off">
...
</form>

If you are worried about this not being valid (X)HTML then you can do the same thing using jQuery:

$("#myForm").attr("autocomplete", "off");
游魂 2024-09-03 10:56:18

对于遇到此“后退缓存”问题的任何人,此博文确实让我明白了这个问题。

For anyone who comes across this "Back-Forward-Cache" problem, this blogpost really enlightened the problem for me.

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