Friday, February 11, 2011

Losing Button.Click events after first partial postback in UpdatePanel

I have a Page that has a single instance of a UserControl that itself has a single UpdatePanel. Inside the UpdatePanel are several Button controls. The Click event for these controls are wired up in the code-behind, in the Init event of the UserControl.

I get the Click event for the first button I push, every time, no problem. After that, I only get Click events for one button (SearchButton) - the rest are ignored. I have included the code for the control below - for sake of brevity, I have excluded the click event handler methods, but they are all of the standard "void Button_Click(object sender, EventArgs e)" variety. Any ideas?

<asp:UpdatePanel ID="PickerUpdatePanel" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Panel ID="Container" runat="server">
            <div>
                <asp:TextBox ID="PickerResults" runat="server" style="margin-right: 3px;" SkinID="Plain" />
                <asp:Image
                    ID="LaunchPopup" runat="server" ImageUrl="~/images/icons/user_manage.png" 
                    ImageAlign="Top" BorderColor="#294254" BorderStyle="Dotted" BorderWidth="1px" 
                    Height="20px" Width="20px" style="cursor: pointer;" />
            </div>
            <asp:Panel ID="PickerPanel" runat="server" DefaultButton="OKButton" CssClass="popupDialog" style="height: 227px; width: 400px; padding: 5px; display: none;">
                <asp:Panel runat="server" id="ContactPickerSearchParams" style="margin-bottom: 3px;" DefaultButton="SearchButton">
                    Search: <asp:TextBox ID="SearchTerms" runat="server" style="margin-right: 3px;" Width="266px" SkinID="Plain" />
                    <asp:Button ID="SearchButton" runat="server" Text="Go" Width="60px" SkinID="Plain" />
                </asp:Panel>
                <asp:ListBox ID="SearchResults" runat="server" Height="150px" Width="100%" SelectionMode="Multiple" style="margin-bottom: 3px;" />
                <asp:Button ID="AddButton" runat="server" Text="Add >>" style="margin-right: 3px;" Width="60px" SkinID="Plain" />
                <asp:TextBox ID="ChosenPeople" runat="server" Width="325px" SkinID="Plain" />
                <div style="float: left;">
                    <asp:Button ID="AddNewContact" runat="server" SkinID="Plain" Width="150px" Text="New Contact" />
                </div>
                <div style="text-align: right;">
                    <asp:Button ID="OKButton" runat="server" Text="Ok" SkinID="Plain" Width="100px" />
                </div>
                <input id="SelectedContacts" runat="server" visible="false" />
            </asp:Panel>
            <ajax:PopupControlExtender ID="PickerPopEx" runat="server" PopupControlID="PickerPanel" TargetControlID="LaunchPopup" Position="Bottom" />
        </asp:Panel>
   </ContentTemplate>
   <Triggers>
        <asp:AsyncPostBackTrigger ControlID="AddButton" EventName="Click" />
        <asp:AsyncPostBackTrigger ControlID="SearchButton" EventName="Click" />
        <asp:AsyncPostBackTrigger ControlID="AddNewContact" EventName="Click" />
    </Triggers>
</asp:UpdatePanel>

public partial class ContactPicker : System.Web.UI.UserControl
{
    protected void Page_Init(object sender, EventArgs e)
    {
        SearchButton.Click += new EventHandler(SearchButton_Click);
        AddButton.Click += new EventHandler(AddButton_Click);
        OKButton.Click += new EventHandler(OKButton_Click);
    }

    // Other code left out
}
  • It seems that adding UseSubmitBehavior="false" to the button definitions has solved my problem. Still don't know why that first button click worked at all.

  • The most likely reason for this would be the client IDs that .Net generates for its controls changing. These are dynamically assigned and could change between postbacks / partial postbacks.

    If controls are being added to the panel dynamically, the ID of your button could be different between postbacks causing .Net to be unable to tie the click event to the correct event handler in your code.

    From Blatfrig
  • and how would one work around this problem, Blatfig?

0 comments:

Post a Comment