locked
Events not firing in ASP.NET user control RRS feed

  • Question

  • User-866885355 posted

    I have a UserControl let's call it ButtonPanel.  This is a panel (id="bPanel") that contains a Literal (id="msg") and a Button (id="btn"  The ButtonPanel class has an event handler ("btn_Click") which is bound to the button in the ASPX tag.  The handler simply puts "Button Pressed" into the literal.

    In my main page, if I drag a ButtonPanel from the Toolbox into the page then when the button is pressed, the event is fired within the control, and the message is shown. All works as expected.

    However, if I dynamically create a ButtonPanel object using the LoadControl function from the main page's Page_Init function, then the btn_Click event handler in the control is never called when the button is clicked.   The user control's Page_Load function is called, so clearly the system knows that some event happened. 

    One would think that the id of "btn" would be included in the postback data so the server would be able to figure out how to dispatch the event, but I have been unable to find it in Request.Params.

    A few additional notes:

    The user controls are created unconditionally every time the main page's Page_Init function is called, so there should be no issue with the event routing failing because the target control doesn't exist.

    What's going on here?  Are user controls fundamentally broken?  Seems like it to me.

    Thursday, November 8, 2018 8:03 PM

All replies

  • User753101303 posted

    Hi,

    Your code includes binding the click handler to the button click event ? Anyway you should use PreInit instead (if I remember it was added to solve or simplify corner cases, as it allows to ensure that all dynamic controls are created when Init starts).

    Thursday, November 8, 2018 8:48 PM
  • User-866885355 posted

    As I said before, the event handler binding is part of the tag in the ASPX file:

    <asp:Button ID="btn" runat="server" Text="Button" OnClick="btn_Click" />

    And I'll say once again, nothing is dynamically created within the user control.  The user control is created dynamically from the main page.

    Thursday, November 8, 2018 11:15 PM
  • User-158764254 posted

    When you dynamically create the UserControl, make sure you are assigning it a specific value to it's ID property.

    If that ID value is not identical on each postback, then the events will not fire correctly.

    myControl.ID = "123";

    Friday, November 9, 2018 1:41 AM
  • User-866885355 posted

    When you dynamically create the UserControl, make sure you are assigning it a specific value to it's ID property.

    If that ID value is not identical on each postback, then the events will not fire correctly.

    myControl.ID = "123";

    The dynamically created controls are assigned ID values in deterministic fashion but it makes no difference either way.  No events are dispatched within the user control.

    Friday, November 9, 2018 6:08 PM
  • User-866885355 posted

    Your code includes binding the click handler to the button click event

    This is done in the ASPX:

    <asp:Button ID="btn" runat="server" Text="Button" OnClick="btn_Click" />

    Anyway you should use PreInit instead (if I remember it was added to solve or simplify corner cases, as it allows to ensure that all dynamic controls are created when Init starts).

    You can't use PreInit to add controls to the page because the objects for controls on the page haven't been created yet.  If I try to access the Panel that is supposed to contain the dynamically created user controls, the object is still NULL.

    Friday, November 9, 2018 7:21 PM