2010-11-01 28 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,这会很麻烦。

相关问题