FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModuleshttp://forums.asp.net/t/1191194.aspx/1?FAQ+Sequence+that+events+are+raised+for+Pages+UserControls+MasterPages+and+HttpModulesSun, 26 Oct 2008 15:35:56 -040011911942046062http://forums.asp.net/p/1191194/2046062.aspx/1?FAQ+Sequence+that+events+are+raised+for+Pages+UserControls+MasterPages+and+HttpModulesFAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules <p>Understanding the Page Life Cycle can be very important as you begin to&nbsp;build Pages with MasterPages and UserControls.</p> <p>Does the Init event fire first for the Page, the MasterPage or the UserControl?&nbsp; <br> What about the Load event?</p> <p>If you make an incorrect assumption about the sequence that these events fire, then you may end up with a&nbsp;page that simply doesn't behave the way you had anticipated.</p> <p>By&nbsp;running a simple test, we can see exactly when each event fires.&nbsp; Our test setup is composed of&nbsp;a Page, MasterPage, UserControl, Nested UserControl and Button control as follows:</p> <ul> <li>The Page is tied to the MasterPage </li><li>The UserControl is on the Page </li><li>The Nested UserControl is on the UserControl </li><li>The Button is on the Nested UserControl. </li><li>Clicking the Button&nbsp;calls the Page.DataBind method </li></ul> <p>Each event on these controls has been set to call <span size="2" style="font-size:small"> Debug.WriteLine as each event is raised.&nbsp; 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&nbsp;of running this page and Clicking the Button are as follows:</span></p> <p>BeginRequest - HttpModule<br> AuthenticateRequest - HttpModule<br> PostAuthenticateRequest - HttpModule<br> PostAuthorizeRequest - HttpModule<br> ResolveRequestCache - HttpModule<br> PostResolveRequestCache - HttpModule<br> PostMapRequestHandler - HttpModule<br> AcquireRequestState - HttpModule<br> PostAcquireRequestState - HttpModule<br> PreRequestHandlerExecute - HttpModule</p> <p>PreInit - Page</p> <p>Init - ChildUserControl<br> Init - UserControl<br> Init - MasterPage<br> Init - Page</p> <p>InitComplete - Page</p> <p>LoadPageStateFromPersistenceMedium - Page&nbsp;</p> <p>ProcessPostData (first try)&nbsp;- Page</p> <p>PreLoad - Page</p> <p>Load - Page<br> Load - MasterPage<br> Load - UserControl<br> Load - ChildUserControl</p> <p>ProcessPostData (second try)&nbsp;- Page</p> <p>RaiseChangedEvents - Page<br> RaisePostBackEvent - Page</p> <p>Click - Button - ChildUserControl</p> <p>&nbsp;&nbsp;&nbsp; DataBinding - Page<br> &nbsp;&nbsp;&nbsp; DataBinding - MasterPage<br> &nbsp;&nbsp;&nbsp; DataBinding - UserControl<br> &nbsp;&nbsp;&nbsp; DataBinding - ChildUserControl</p> <p>LoadComplete - Page</p> <p>PreRender - Page<br> PreRender - MasterPage<br> PreRender - UserControl<br> PreRender - ChildUserControl</p> <p>PreRenderComplete - Page</p> <p>SaveViewState - Page<br> SavePageStateToPersistenceMedium - Page<br> SaveStateComplete - Page</p> <p>Unload - ChildUserControl<br> Unload - UserControl<br> Unload - MasterPage<br> Unload - Page</p> <p>PostRequestHandlerExecute - HttpModule<br> ReleaseRequestState - HttpModule<br> PostReleaseRequestState - HttpModule<br> UpdateRequestCache - HttpModule<br> PostUpdateRequestCache - HttpModule<br> EndRequest - HttpModule<br> PreSendRequestHeaders - HttpModule<br> PreSendRequestContent - HttpModule&nbsp;</p> <p>&nbsp;</p> <p>I hope that you will find this information useful.</p> <p>&nbsp;</p> 2007-12-07T01:13:45-05:002046395http://forums.asp.net/p/1191194/2046395.aspx/1?Re+FAQ+Sequence+that+events+are+raised+for+Pages+UserControls+MasterPages+and+HttpModulesRe: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules <p>&nbsp;Thanks Mike,</p> 2007-12-07T05:48:32-05:002059570http://forums.asp.net/p/1191194/2059570.aspx/1?Re+FAQ+Sequence+that+events+are+raised+for+Pages+UserControls+MasterPages+and+HttpModulesRe: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules <p>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 ?&nbsp; sorry if its a very naive question. </p> 2007-12-14T06:46:40-05:002060187http://forums.asp.net/p/1191194/2060187.aspx/1?Re+FAQ+Sequence+that+events+are+raised+for+Pages+UserControls+MasterPages+and+HttpModulesRe: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules <p></p> <blockquote><span class="icon-blockquote"></span> <h4>slsp</h4> Don't all the events on page, usercontrol, master page, etc, fire without even wiring up with HttpModule as well ?&nbsp;</blockquote> <p>Yes - those events would fire in that same sequence&nbsp;even without the HttpModule. </p> <p>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&nbsp;they relate to the page life cycle. </p> <p>&nbsp;</p> 2007-12-14T13:43:34-05:002426130http://forums.asp.net/p/1191194/2426130.aspx/1?Re+FAQ+Sequence+that+events+are+raised+for+Pages+UserControls+MasterPages+and+HttpModulesRe: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules <p>Hey Mike </p> <p>&nbsp;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. <a href="http://www.eggheadcafe.com/articles/20051227.asp">http://www.eggheadcafe.com/articles/20051227.asp</a>. In 2 respect one without a postback and one with postback. </p> <p>&nbsp;Help is highly apreciated.</p> <p>Regards</p> <p>Mohit Thakral</p> <p>&nbsp;</p> 2008-06-16T15:53:11-04:002546150http://forums.asp.net/p/1191194/2546150.aspx/1?Re+FAQ+Sequence+that+events+are+raised+for+Pages+UserControls+MasterPages+and+HttpModulesRe: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules <p>I have a page with some dynamically added controls.&nbsp; 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?</p> <p>pamela</p> 2008-08-08T15:37:41-04:002546788http://forums.asp.net/p/1191194/2546788.aspx/1?Re+FAQ+Sequence+that+events+are+raised+for+Pages+UserControls+MasterPages+and+HttpModulesRe: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules <p>The Load events for the child controls is not affected by using If Not IsPostBack in the load event of the master page.</p> <p>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.&nbsp; Give this faq a read to find some tips on getting your dynamic controls working: <a href="http://forums.asp.net/t/1186195.aspx">http://forums.asp.net/t/1186195.aspx</a></p> 2008-08-08T22:24:04-04:002546803http://forums.asp.net/p/1191194/2546803.aspx/1?Re+FAQ+Sequence+that+events+are+raised+for+Pages+UserControls+MasterPages+and+HttpModulesRe: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules <p>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.</p> <p>The link mosessaur gave me to <a href="http://odetocode.com/Blogs/scott/archive/2006/07/19/5365.aspx"> Dynamically Loading Controls in ASP.NET is A Pain</a> explained the problem and the solution fairly well.</p> <p>Thanks for the additional info</p> <p>pamela</p> 2008-08-08T22:39:14-04:002546811http://forums.asp.net/p/1191194/2546811.aspx/1?Re+FAQ+Sequence+that+events+are+raised+for+Pages+UserControls+MasterPages+and+HttpModulesRe: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules <p>the load events will fire.&nbsp; When controls are loaded using LoadControl, the framework will catch up their life cycle events to the page they are being loaded into.&nbsp; if you load them too late though, they will not fire click events and process viewstate correctly.</p> 2008-08-08T22:44:20-04:002689553http://forums.asp.net/p/1191194/2689553.aspx/1?Re+FAQ+Sequence+that+events+are+raised+for+Pages+UserControls+MasterPages+and+HttpModulesRe: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules <p>Thanks for this post, it is very good for a starter like me.</p> 2008-10-17T11:05:13-04:002699516http://forums.asp.net/p/1191194/2699516.aspx/1?Re+FAQ+Sequence+that+events+are+raised+for+Pages+UserControls+MasterPages+and+HttpModulesRe: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules <p>Hi </p> <p>Thanks a lot nice sequence steps about events but what about LoadPostbackData and LoadViewStateData ?</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;&nbsp; <br> </p> 2008-10-22T12:47:16-04:002706886http://forums.asp.net/p/1191194/2706886.aspx/1?Re+FAQ+Sequence+that+events+are+raised+for+Pages+UserControls+MasterPages+and+HttpModulesRe: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules <p>I've added a few additional events to the&nbsp;original post&nbsp;for you:</p> <ul> <li>&lt;div mce_keep=&quot;true&quot;&gt;LoadPageStateFromPersistenceMedium &lt;/div&gt; </li><li>&lt;div mce_keep=&quot;true&quot;&gt;ProcessPostData (first try)&lt;/div&gt; </li><li>&lt;div mce_keep=&quot;true&quot;&gt;ProcessPostData (second try)&lt;/div&gt; </li><li>RaiseChangedEvents </li><li>RaisePostBackEvent </li><li>SaveViewState </li><li>SavePageStateToPersistenceMedium<br> </li></ul> 2008-10-26T15:35:56-04:00