[web referrence] Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules

originally from:
http://forums.asp.net/t/1191194.aspx

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:

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

PreLoad - Page

Load - Page
Load - MasterPage
Load - UserControl
Load - ChildUserControl

Click - Button - ChildUserControl

    DataBinding - Page
    DataBinding - MasterPage
    DataBinding - UserControl
    DataBinding - ChildUserControl

LoadComplete - Page

PreRender - Page
PreRender - MasterPage
PreRender - UserControl
PreRender - ChildUserControl

PreRenderComplete - 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.

 

Mike Banavige
~~~~~~~~~~~~
Dont forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
posted @ 2008-01-14 23:56  N/A2011  阅读(365)  评论(2编辑  收藏  举报