Firing SelectedIndexChanged event , but the listbo

2019-08-03 09:19发布

问题:

I am using an update panel . In this update panel, there is a listbox control. I actually set autopostback property to false in code behind. But still it executing SelectedIndexChanged event if the selected index is changed.

Why this happens?

    <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>            

The listbox name is lbAvailableForms. While debugging i checked the autopostback property of this list box control, then i found that the property is false. Its looking so strange then how the selectedindexchanged event firing

Here cbAllForm is a check box control and RLCompareParameteris a radilo list.

Sometimes i need to get auto postback property is true. So initially i set this property to true. under RLCompareParameter_SelectedIndexChanged event , i set lbAvailableForms.Autopostback=false. But still after setting the property to false ,the listbox firing selected indexchanged event

回答1:

It might be too late to change the AutoPostBack property in your event handling phase: the UpdatePanel may already have registered its triggers.

I would start by disabling AutoPostBack and ViewState (which remembers AutoPostBack) on the list box:

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

Then introduce a private member to keep track of what we want to do and set that member in the event handler:

private bool _disableAutoPostBack = false;

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

Then use it in the PreRender phase afterwards:

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

Then hope it works as it is, so we don't have to dynamically register an AsyncPostBackTrigger on the ListBox, which would be messy.