【问题标题】:DropDownList SelectedIndex not working in FireFox after page refresh页面刷新后,DropDownList SelectedIndex 在 FireFox 中不起作用
【发布时间】:2010-03-30 14:09:59
【问题描述】:

我在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。但是,当我通过将光标放在地址栏中并按回车刷新页面时一切正常(drowdownlist显示 0)。该问题仅在 FireFox 中出现(我有 3.5.7 版)。

有什么想法会导致这个问题吗?

【问题讨论】:

    标签: firefox drop-down-menu updatepanel selectedindex


    【解决方案1】:

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

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

    有效!

    在 PHP 中:

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

    【讨论】:

      【解决方案2】:

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

      例如。

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

      如果您担心这不是有效的 (X)HTML,那么您可以使用 jQuery 做同样的事情:

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

      【讨论】:

        【解决方案3】:

        对于遇到这个“Back-Forward-Cache”问题的任何人,this blogpost 真的为我解决了这个问题。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-08-15
          • 2011-03-11
          • 2014-08-23
          • 2020-02-15
          • 1970-01-01
          • 2019-02-20
          • 2019-02-24
          相关资源
          最近更新 更多