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:
The Page is tied to the MasterPage
The UserControl is on the Page
The Nested UserControl is on the UserControl
The Button is on the Nested UserControl.
Clicking the Button calls the Page.DataBind method
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:
Dont forget to click “Mark as Answer” on the post that helped you.
This credits that member, earns you a point and mark your thread as Resolved for the sake of Future Readers.
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.
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.
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.
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?
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:
http://forums.asp.net/t/1186195.aspx
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 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.
All-Star
160090 Points
13198 Posts
ASPInsiders
Moderator
FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules
Dec 06, 2007 09:13 PM|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.
Member
314 Points
125 Posts
Re: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules
Dec 07, 2007 01:48 AM|Deepesh|LINK
Thanks Mike,
This credits that member, earns you a point and mark your thread as Resolved for the sake of Future Readers.
Member
20 Points
15 Posts
Re: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules
Dec 14, 2007 02: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.
life cycle
All-Star
160090 Points
13198 Posts
ASPInsiders
Moderator
Re: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules
Dec 14, 2007 09:43 AM|mbanavige|LINK
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.
None
0 Points
1 Post
Re: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules
Jun 16, 2008 11:53 AM|MohitT|LINK
Hey Mike
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.
Regards
Mohit Thakral
Member
1 Points
55 Posts
Re: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules
Aug 08, 2008 11:37 AM|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?
pamela
All-Star
160090 Points
13198 Posts
ASPInsiders
Moderator
Re: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules
Aug 08, 2008 06: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: http://forums.asp.net/t/1186195.aspx
Member
1 Points
55 Posts
Re: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules
Aug 08, 2008 06: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
pamela
All-Star
160090 Points
13198 Posts
ASPInsiders
Moderator
Re: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules
Aug 08, 2008 06: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.
Member
11 Points
36 Posts
Re: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules
Oct 17, 2008 07:05 AM|Mr. Javaman|LINK
Thanks for this post, it is very good for a starter like me.
Member
1 Points
21 Posts
Re: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules
Oct 22, 2008 08:47 AM|jaiprakashv|LINK
Hi
Thanks a lot nice sequence steps about events but what about LoadPostbackData and LoadViewStateData ?
Asp.NET2.0
www.interviewsworld.com
All-Star
160090 Points
13198 Posts
ASPInsiders
Moderator
Re: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules
Oct 26, 2008 11:35 AM|mbanavige|LINK
I've added a few additional events to the original post for you: