FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules

Rate It (8)

Last post 12-25-2008 1:13 AM by sirdneo. 12 replies.

Sort Posts:

  • FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules

    12-06-2007, 9:13 PM
    • All-Star
      100,818 point All-Star
    • mbanavige
    • Member since 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 10,509
    • Moderator
      TrustedFriends-MVPs

    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

    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.

     

    Mike Banavige
    ~~~~~~~~~~~~
    Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
  • Re: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules

    12-07-2007, 1:48 AM
    • Member
      569 point Member
    • Deepesh
    • Member since 07-21-2006, 7:31 AM
    • Posts 128

     Thanks Mike,

    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.
  • Re: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules

    12-14-2007, 2:46 AM
    • Member
      40 point Member
    • slsp
    • Member since 12-14-2007, 7:27 AM
    • Posts 16

    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.

    Filed under:
  • Re: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules

    12-14-2007, 9:43 AM
    • All-Star
      100,818 point All-Star
    • mbanavige
    • Member since 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 10,509
    • Moderator
      TrustedFriends-MVPs

    slsp:
    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.

     

    Mike Banavige
    ~~~~~~~~~~~~
    Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
  • Re: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules

    06-16-2008, 11:53 AM
    • Member
      2 point Member
    • MohitT
    • Member since 06-16-2008, 3:42 PM
    • Posts 1

    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

     

  • Re: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules

    08-08-2008, 11:37 AM
    • Member
      11 point Member
    • pthalacker
    • Member since 05-06-2008, 5:18 PM
    • Posts 53

    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

  • Re: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules

    08-08-2008, 6:24 PM
    • All-Star
      100,818 point All-Star
    • mbanavige
    • Member since 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 10,509
    • Moderator
      TrustedFriends-MVPs

    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

    Mike Banavige
    ~~~~~~~~~~~~
    Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
  • Re: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules

    08-08-2008, 6:39 PM
    • Member
      11 point Member
    • pthalacker
    • Member since 05-06-2008, 5:18 PM
    • Posts 53

    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

  • Re: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules

    08-08-2008, 6:44 PM
    • All-Star
      100,818 point All-Star
    • mbanavige
    • Member since 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 10,509
    • Moderator
      TrustedFriends-MVPs

    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.

    Mike Banavige
    ~~~~~~~~~~~~
    Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
  • Re: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules

    10-17-2008, 7:05 AM
    • Member
      25 point Member
    • Mr. Javaman
    • Member since 10-13-2008, 12:14 PM
    • Posts 36

    Thanks for this post, it is very good for a starter like me.

  • Re: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules

    10-22-2008, 8:47 AM
    • Member
      34 point Member
    • jaiprakashv
    • Member since 08-11-2008, 9:21 AM
    • Gurgaon
    • Posts 21

    Hi

    Thanks a lot nice sequence steps about events but what about LoadPostbackData and LoadViewStateData ?

     

     

      

    Filed under:
  • Re: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules

    10-26-2008, 11:35 AM
    • All-Star
      100,818 point All-Star
    • mbanavige
    • Member since 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 10,509
    • Moderator
      TrustedFriends-MVPs

    I've added a few additional events to the original post for you:

    • LoadPageStateFromPersistenceMedium
    • ProcessPostData (first try)
    • ProcessPostData (second try)
    • RaiseChangedEvents
    • RaisePostBackEvent
    • SaveViewState
    • SavePageStateToPersistenceMedium
    Mike Banavige
    ~~~~~~~~~~~~
    Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
  • Re: FAQ: Sequence that events are raised for Pages, UserControls, MasterPages and HttpModules

    12-25-2008, 1:13 AM
    • Contributor
      7,456 point Contributor
    • sirdneo
    • Member since 12-16-2008, 5:45 AM
    • Karachi, Pakistan
    • Posts 1,185

     Very usefull post I was looking for such compherisensive set of events for long time. NIce work.

    Thanks,
    Zeeshan Umar

    ~Please Mark As Answer, if one or multiple posts, which helped you in your problem. So that it might be useful for others~

    My Blog
Page 1 of 1 (13 items)