Mark-up Button Event Not Firing in ASP.NET (Details inside)
There is nothing fancy with the mark-up:
<hr />
Add... <asp:Button ID="buttonTextSegment" runat="server" Text="Text Segment"
onclick="buttonTextS开发者_Go百科egment_Click" />
<hr />
Or the code:
protected void buttonTextSegment_Click(object sender, EventArgs e)
{
//never is triggered
}
My guess is that it is due to the hierarchy/load order:
In English: Inside the page's Load, it adds a user control. Inside of that user control, inside of Page_LoadComplete (is this the issue!?), it adds another user control, it is the inner-most user control that contains the mark-up button.
Pseudo Visually: Page -> Page_Load: Dynamically Added User Control -> Page_LoadComplete: Another Dynamically Added User Control -> User Control Mark-Up: Button with event.
Do events need to be hooked before LoadComplete? (Is there a way to make events still work even though the controls are added in LoadComplete?)
You should create the user controls in this order Page -> Page_Init: Dynamically Added User Control -> Page_Init: Another Dynamically Added User Control -> User Control Markup.
If you do them in the Page_Load or Page_LoadComplete functions then they are created too late for the event handlers as the event handlers are fired directly after the LoadControlState event - which happens between the Page_Init and the Page_Load events.
精彩评论