开发者

DropDownList SelectedIndex not working in FireFox after page refresh

开发者 https://www.devze.com 2022-12-25 06:35 出处:网络
I\'ve got DropDownList in UpdatePanel as shown below: <asp:UpdatePanel ID=\"UpdatePanel1\" runat=\"server\">

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 everyt开发者_如何学运维hig 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?


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");
?>


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");


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

0

精彩评论

暂无评论...
验证码 换一张
取 消