Last post Oct 26, 2008 03:35 PM by mbanavige
Dec 07, 2007 01:13 AM|mbanavige|LINK
Understanding the Page Life Cycle can be very important as you begin to build Pages with MasterPages and UserControls.
Does the Init event fire first for the Page, the MasterPage or the UserControl?
What about the Load event?
If you make an incorrect assumption about the sequence that these events fire, then you may end up with a page that simply doesn't behave the way you had anticipated.
By running a simple test, we can see exactly when each event fires. Our test setup is composed of a Page, MasterPage, UserControl, Nested UserControl and Button control as follows:
Each event on these controls has been set to call
Debug.WriteLine as each event is raised. In addition to the events that get raised for regular pages, I've also set up an HttpModule and wired up all of those events as well. The results in the Debug output window of running this page and Clicking the Button
are as follows:
BeginRequest - HttpModule
AuthenticateRequest - HttpModule
PostAuthenticateRequest - HttpModule
PostAuthorizeRequest - HttpModule
ResolveRequestCache - HttpModule
PostResolveRequestCache - HttpModule
PostMapRequestHandler - HttpModule
AcquireRequestState - HttpModule
PostAcquireRequestState - HttpModule
PreRequestHandlerExecute - HttpModule
PreInit - Page
Init - ChildUserControl
Init - UserControl
Init - MasterPage
Init - Page
InitComplete - Page
LoadPageStateFromPersistenceMedium - Page
ProcessPostData (first try) - Page
PreLoad - Page
Load - Page
Load - MasterPage
Load - UserControl
Load - ChildUserControl
ProcessPostData (second try) - Page
RaiseChangedEvents - Page
RaisePostBackEvent - Page
Click - Button - ChildUserControl
DataBinding - Page
DataBinding - MasterPage
DataBinding - UserControl
DataBinding - ChildUserControl
LoadComplete - Page
PreRender - Page
PreRender - MasterPage
PreRender - UserControl
PreRender - ChildUserControl
PreRenderComplete - Page
SaveViewState - Page
SavePageStateToPersistenceMedium - Page
SaveStateComplete - Page
Unload - ChildUserControl
Unload - UserControl
Unload - MasterPage
Unload - Page
PostRequestHandlerExecute - HttpModule
ReleaseRequestState - HttpModule
PostReleaseRequestState - HttpModule
UpdateRequestCache - HttpModule
PostUpdateRequestCache - HttpModule
EndRequest - HttpModule
PreSendRequestHeaders - HttpModule
PreSendRequestContent - HttpModule
I hope that you will find this information useful.
Dec 07, 2007 05:48 AM|Deepesh|LINK
Dec 14, 2007 06:46 AM|slsp|LINK
Thanks a lot for this post. It sure cleared some confusions. But can u please explain where this HttpModule comes into picture. Don't all the events on page, usercontrol, master page, etc, fire without even wiring up with HttpModule as well ? sorry if its
a very naive question.
Dec 14, 2007 01:43 PM|mbanavige|LINK
Don't all the events on page, usercontrol, master page, etc, fire without even wiring up with HttpModule as well ?
Yes - those events would fire in that same sequence even without the HttpModule.
I included the events from the HttpModule as a person developing an HttpModule might be interested to see the sequence of the HttpModule events and how they relate to the page life cycle.
Jun 16, 2008 03:53 PM|MohitT|LINK
I really apreciate the way u wrote the post in simple and concise manner. I have only one question are these event in 1.1 or 2.0 and what about LoadPostbackData and LoadViewStateData. Can you please explain. And can you please explain the events in the
article. http://www.eggheadcafe.com/articles/20051227.asp. In 2 respect one without a postback and one with postback.
Help is highly apreciated.
Aug 08, 2008 03:37 PM|pthalacker|LINK
I have a page with some dynamically added controls. My MasterPage control-loading code is wrapped in an If Not IsPostback condition so naturally none of the load events of the child controls fire, but it appears that the critical button-click of the child
controls doesn't fire either.Is that to be expected?
Aug 08, 2008 10:24 PM|mbanavige|LINK
The Load events for the child controls is not affected by using If Not IsPostBack in the load event of the master page.
The more likely cause for your load events not firing and button-click events not firing has to do with your controls being dynamically added to your page. Give this faq a read to find some tips on getting your dynamic controls working:
Aug 08, 2008 10:39 PM|pthalacker|LINK
Actually they are affected, since the child controls are dynamically loaded from the page.Load event and if they are not loaded, their Load events probably won't fire.
The link mosessaur gave me to
Dynamically Loading Controls in ASP.NET is A Pain explained the problem and the solution fairly well.
Thanks for the additional info
Aug 08, 2008 10:44 PM|mbanavige|LINK
the load events will fire. When controls are loaded using LoadControl, the framework will catch up their life cycle events to the page they are being loaded into. if you load them too late though, they will not fire click events and process viewstate correctly.
Oct 17, 2008 11:05 AM|Mr. Javaman|LINK
Thanks for this post, it is very good for a starter like me.
Oct 22, 2008 12:47 PM|jaiprakashv|LINK
Thanks a lot nice sequence steps about events but what about LoadPostbackData and LoadViewStateData ?
Oct 26, 2008 03:35 PM|mbanavige|LINK
I've added a few additional events to the original post for you: