2010-11-01 39 views
1

我正在使用更新面板。在此更新面板中,有一個列表框控件。我實際上在後面的代碼中將autopostback屬性設置爲false。但如果所選索引改變,它仍然執行SelectedIndexChanged事件。發射SelectedIndexChanged事件,但列表框的autopostback屬性爲false - ASP.NET

爲什麼會發生這種情況?

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> 
             <ContentTemplate> 

              <asp:MultiView ID="mvForms" runat="server" ActiveViewIndex="1"> 
               <asp:View ID="View1" runat="server"> 
                <asp:Panel ID="Panel5" runat="server" GroupingText="Available Evaluation Forms" meta:resourcekey="rsKey_panel5" 
                Width="100%"> 
                 <asp:ListBox ID="lbAvailableForms" runat="server" AutoPostBack="true" 
                  style="height: 125px; width: 95%;" 
                  onselectedindexchanged="lbAvailableForms_SelectedIndexChanged"></asp:ListBox> 
                 </asp:Panel> 
               </asp:View> 
               <asp:View ID="View2" runat="server"> 
                <asp:Panel ID="Panel11" runat="server" GroupingText="Available Evaluation Forms" meta:resourcekey="rsKey_panel11"  Width="100%"> 
                 <div style="height: 125px; width: 95%; text-align:center;"> 
                  <br /> 
                  <br /> 
                  <asp:Label ID="lblAllSelected" runat="server" Text="All Selected" meta:resourcekey="rsKey_lblAllSelected"></asp:Label></div> 
                </asp:Panel> 
               </asp:View> 
              </asp:MultiView> 
           </ContentTemplate> 
          <Triggers>  
          <asp:AsyncPostBackTrigger ControlID="RLCompareParameter" EventName="SelectedIndexChanged"></asp:AsyncPostBackTrigger>   
          <asp:AsyncPostBackTrigger ControlID="cbAllForms" EventName="CheckedChanged"></asp:AsyncPostBackTrigger> 
         </Triggers> 
         </asp:UpdatePanel>    

列表框名稱是lbAvailableForms。在調試時,我檢查了這個列表框控件的autopostback屬性,然後我發現該屬性爲false。它看起來很奇怪,然後如何selectedindexchanged事件觸發

這裏cbAllForm是一個複選框控件,RLCompareParameter是一個radilo列表。

有時我需要得到自動回發屬性是正確的。所以最初我把這個屬性設置爲true。在RLCompareParameter_SelectedIndexChanged事件下,我設置了lbAvailableForms.Autopostback = false。但在將屬性設置爲false後,列表框觸發選定的indexchanged事件

+0

請發佈您的代碼的相關部分。請注意,在執行普通回發後,SelectedIndexChanged也會觸發。 – cbp 2010-11-01 06:21:51

+0

謝謝你的回覆。我添加了一些代碼。 – 2010-11-01 06:33:55

+0

什麼是'cbAllForms'?如果將'UpdatePanel1'的[ChildrenAsTriggers](http://msdn.microsoft.com/zh-cn/library/system.web.ui.updatepanel.childrenastriggers.aspx)屬性設置爲'false',問題是否會持續存在? – 2010-11-01 06:45:20

回答

2

在事件處理階段更改AutoPostBack屬性可能爲時已晚:UpdatePanel可能已經註冊了它的觸發器。

我會通過禁用AutoPostBackViewState列表框(它可以記住AutoPostBack)開始:

<asp:ListBox ID="lbAvailableForms" runat="server" 
    AutoPostBack="False" EnableViewState="False" 
    Style="height: 125px; width: 95%;" 
    OnSelectedIndexChanged="lbAvailableForms_SelectedIndexChanged"> 
</asp:ListBox> 

再介紹一個私有成員跟蹤的是我們要在事件做,並設置成員處理:

private bool _disableAutoPostBack = false; 

protected void RLCompareParameter_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    _disableAutoPostBack = true; 
} 

然後在PreRender階段使用它算賬:

protected void Page_PreRender(object sender, EventArgs e) 
{ 
    lbAvailableForms.AutoPostBack = !_disableAutoPostBack; 
} 

然後希望它可以正常工作,所以我們不必在ListBox上動態註冊AsyncPostBackTrigger,這會很麻煩。

相關問題