Losing Button.Click events after the first partial postback in UpdatePanel

I have a page in which there is one instance of UserControl in which there is one UpdatePanel. There are several Button controls inside the UpdatePanel. The Click event for these controls connects to the code in the Init event for the UserControl.

I get a Click event for the first button that I click, every time, no problem. After that, I get only Click events for one button (SearchButton) - the rest are ignored. I have included code for control below - for the sake of brevity, I have excluded click event handler methods, but they are all standard "void Button_Click (object sender, EventArgs e)". 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 } 
+4
source share
3 answers

It seems that adding UseSubmitBehavior = "false" to the button definitions solved my problem. Still don't know why this first button click worked at all.

+4
source

The most likely reason for this may be the client identifiers that .Net generates to modify its controls. They are dynamically assigned and can change between postbacks / partial postbacks.

If the controls are added dynamically to the panel, the identifier of your button may differ from mail messages, as a result of which .Net cannot bind the click event to the correct event handler in your code.

+1
source

In my case, I had a LinkButton inside the LinkButton event handler that used the PostBackUrl property.

The moment I changed LinkButton to a HyperLink , the problem disappeared.

0
source

Source: https://habr.com/ru/post/1277546/


All Articles