2010-03-30 37 views
3

我得的DropDownList在UpdatePanel中,如下圖所示:的DropDownList的SelectedIndex不能在Firefox工作頁面刷新後

<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版本)。

任何想法可以導致這個問題?

回答

3

Firefox會記住會話中每個select的selectedIndex。這對用戶來說很好,但對開發者來說卻是一件麻煩事......我遇到了同樣的問題。如果我找到解決方案,我會發布它。

檢查了這一點: https://developer.mozilla.org/en/Using_Firefox_1.5_caching

它的工作原理!

在PHP:

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

謝謝詹姆斯,是解決我的問題。 – jartur 2010-04-20 11:09:48

0

對於任何人誰碰到這個「回到轉發數據緩存」問題就來了,this blogpost真是不看不知道這個問題對我來說。

1

您可以在您的表單中添加名爲autocomplete的屬性並將其設置爲off以防止在Firefox中發生此行爲。我發現這是解決這個問題最簡單的方法。

例如。

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

如果你擔心這不是有效的(X)HTML,那麼你可以做使用jQuery同樣的事情:

$("#myForm").attr("autocomplete", "off"); 
相關問題