In my web application I load user controls in the page_loadComplete event. This works fine, however when a button is clicked in a user control, the click event is never fired. Does this has something to do with the page lifecycle? That button click (UI) events occur before the LoadComplete event?
Event Handling in ASP.NET page happens after Validation and before Rendering phase. And Validation phase happens after Load.
LoadComplete happens after Control events and before RreRender event.
Actually what is happening to your situation is, when you click on the button, before event raising, LoadCoplete event fire first in the page lifecycle and same control is again created and here is your event is lost.
You need to make sure the click event of the button is once again subscribed to, before event handlers fire. LoadComplete happens after control events. For a reference, the ASP.NET Page Life Cycle Overview gives a pretty nice summary.
Snippet:
- ...
- Load
- Control events
- LoadComplete
- PreRender
- ...
You also need to make sure that the controls that you dynamically load all end up in the same place, so viewstate and controlstate can be reapplied to the same hierarchy as before postback.
Basically, you need to load all dynamic controls on each postback.
Here's someone with the same problem, and solutions to some of them: ASP.NET dynamic controls