Locations of visitors to this page
    ...       Minimize  

Below is the in-depth listing of all the steps of the ASP.NET LifeCycle -- which I consider to be essential knowledge for any person working in ASP.NET.

If you haven't read the associated article , introducing and describing the essence of the ASP.NET LifeCycle, and the main parts to keep an eye out for in the following chart, you can find it in the Articles Section.

If you decide to skip the article and just plunge in, read the comments on the right step... You should be paying special attention to when ViewStating is being turned on, and when it is being turned off: this is useful for reasons discussed in this other article about CompositeControl and property wiring...

 


             
    The ASP.NET Control/Page LifeCycle       Minimize  
Legend
Platform Type Request Class
1.1 Method All Page
2.0 Event Initial Only Control
PostBack Only Sub Control
HttpApplication


* * * Object Method/Event Name Description/Remark
Stage: Pipeline
At the highest level of abstraction, the ASP.NET worker process accomplishes one main task: handing the request over to a chain of managed objects dubbed the HTTP pipeline. The HTTP pipeline is activated by creating a new instance of the HttpRuntime class, and then calling its ProcessRequest method.

In essence, there is one HttpApplication assigned per virtual directory/WebApplication, whose job -- among many -- is to receive a request for an url (eg: 'http://MyApp/page1.aspx'), and from the extension ('aspx') find the right IHttpHandler-based class to process it.

Once an IHttpHandler based class (ie, Page in most cases) is found, the Handler is instantiated, its ProcessRequest method is called -- which recursively instantiates itself, processes post back data, renders itself, and all child controls, saves viewstate, then returns control to the HttpApplication in order to process the next request.

1.1 HttpRuntime
>ProcessRequest
1.1 HttpApplication
>OnStart
Raised when ASP.NET responds to a first request.
In other words, only once per session...
1.1 HttpApplication
>BeginRequest
Raised when ASP.NET responds to a new request.
1.1 HttpApplication
>AuthenticateRequest
Raised when a security module has established the identity of the user.
1.1 HttpApplication
>AuthorizeRequest
Raised when a security module has verified user authorization.
1.1 HttpApplication
>ResolveRequestCache
Raised to let caching modules serve request from the cache if any, bypassing execution of Page(IHttpHandler).
A handler (a Page class corresponding to the requested Url) is created at this point.
1.1 HttpApplication
>Session_Start
Raised when a new session is started.
In other words, only once per session...
1.1 HttpApplication
>AcquireRequestState
Raised just before ASP.NET begins executing a Page(IHttpHandler).
1.1 HttpApplication
>PreRequestHandlerExecute
Raised just before ASP.NET begins executing a Page(IHttpHandler).
Stage: Initialization
1.1 Page
AddParsedSubObject
Called when a declarative XML/HTML tag in Control(Page) was parsed, instantiated, and ready to add to Control's ControlCollection.
1.1 Page
CreateControlCollection
Creates a new ControlCollection to hold child controls (both literal and dynamically added).
1.1 Page
AddedControl
Called when a control is added to Control's(Page's) ControlCollection.
2.0 Page
ResolveAdapter
Returns the control adapter responsible for rendering Control(Page) on requesting device or browser.
As ASP.Net page can be viewed on various devices (Browser, Mobile, etc.).
1.1 Page
>ProcessRequest
Method called by HttpApplication that causes generation of page.
1.1 Page
> >ProcessRequestMain
Main method of Page. Calls all the other phases of the Page:
IMPORTANT:
****THE**** method to watch: this is the Page's main dispatcher...

//PsuedoCode:
private void ProcessRequestMain(bool includeStagesBeforeAsyncPoint, bool includeStagesAfterAsyncPoint){
  this._requestValueCollection = this.DeterminePostBackMode();
  if (this.MaintainScrollPositionOnPostback){this.LoadScrollPosition();}
  this.PerformPreInit();
  this.InitRecursive(null);
  this.OnInitComplete(EventArgs.Empty);
  if (this.IsPostBack){
    this.LoadAllState();
    this.ProcessPostData(this._requestValueCollection, true);
  }
  this.OnPreLoad(EventArgs.Empty);
  this.LoadRecursive();
  if (this.IsPostBack){
    this.ProcessPostData(this._leftoverPostData, false);
    this.RaiseChangedEvents();
    this.RaisePostBackEvent(this._requestValueCollection);
  }
  this.OnLoadComplete(EventArgs.Empty);
  this.PreRenderRecursiveInternal();
  this.PerformPreRenderComplete();
  this.SaveAllState();
  this.OnSaveStateComplete(EventArgs.Empty);
  this.RenderControl(this.CreateHtmlTextWriter(this.Response.Output));
}

			
1.1 Page
> > >DeterminePostBackMode
Determines type of request made for the Page (GET, POST, or null).
  • If Initial, returns null.
  • If PostBack, and POST method used, form information is returned from Context object.
  • If GET used, the query string information is returned.
1.1 Page
> > >PerformPreInit
##########

//PsuedoCode:
private void PerformPreInit(){
  this.OnPreInit(EventArgs.Empty);
  this.InitializeThemes();
  this.ApplyMasterPage();
  this._preInitWorkComplete = true;
}
			
2.0 Page
> > > >OnPreInit
Called at beginning of Page initialization.
Raises PreInit event.
Available in Page only.
Relevant info: Only assign a value to MasterPageFile before or during PreInit event.
This restriction makes sense, since different Master Pages = different controls being loaded).

void Page_PreInit(object sender, e As EventArgs){
  MasterPageFile = Profile.Master;
}

			
2.0 Page
> > > > >PreInit
Raised at beginning of Page initialization.
See remarks for OnPreInit.
2.0 Page
> > > >InitializeThemes
##########
2.0 Page
> > > >ApplyMasterPage
##########
1.1 Page
> > >InitRecursive
##########
Calls Page.ResolveAdapter, then recurses (top down) through child controls, setting child control's Page, NamingContainer, and ID property (if one was not set explicitly).
Finally, it calls:

//PsuedoCode:
void InitRecursive(Control namingContainer){
  this.ResolveAdapter();
  foreach (Control childControl in this.Controls){
    childControl._Parent = this;
    childControl._NamingContainer = namingContainer;
    if (childControl._ID == null){
    	childControl._ID = GenerateAutomaticID();
    }
    childControl.InitRecursive(this.NamingContainer);
  }
  ApplySkin(this.Page);
  this.OnInit(...);
  this.z;
}
2.0 Control
> > > >ResolveAdapter
Returns control adapter responsible for rendering Control on specific requesting device.
2.0 [Sub]Control
> > > >ResolveAdapter
Returns control adapter responsible for rendering Control on specific requesting device.
2.0 [Sub]Control
> > > >ApplySkin
##########
1.1 [Sub]Control
> > > > >OnInit
Called after Control and all children have been initialized. Raises Init event.
Is called prior to parent's OnInit. LoadViewState not called yet: VState is not yet populated with previous request's vars. Too early to refer to parent or any child controls.
1.1 [Sub]Control
> > > >Init
Raised after Control and all children have been initialized.
Is called prior to parent's OnInit.
LoadViewState not called yet: VState is not yet populated with previous request's vars.
Too early to refer to parent or any child controls.
1.1 [Sub]Control
> > > >TrackViewState
Starts tracking changes to VState state bag.
IMPORTANT:
Any changes made to VState of Control prior to this are not serialized.
LoadViewState not called yet: VState is not yet populated with previous request's vars.
2.0 Control
> > > >ApplySkin
##########
1.1 Control
> > > >OnInit
Called after Control and all children have been initialized. Raises Init event.
Is called prior to parent's OnInit.
LoadViewState not called yet: VState is not yet populated with previous request's vars.
Too early to refer to parent or any child controls.
1.1 Control
> > > > >Init
Raised after Control and all children have been initialized.
See remarks for OnInit.
Is called prior to parent's OnInit.
LoadViewState not called yet: VState is not yet populated with previous request's vars.
Too early to refer to parent or any child controls.
1.1 Control
> > > >TrackViewState
Starts tracking changes to VState stage bag.
Any changes made to VState of Control prior to this are not serialized.
LoadViewState not called yet: VState is not yet populated with previous request's vars.
2.0 Page
> > > >ApplySkin
##########
1.1 Page
> > > >OnInit
Called after Control and all children have been initialized. Raises Init event.
Is called prior to parent's OnInit.
LoadViewState not called yet: VState is not yet populated with previous request's vars.
Too early to refer to parent or any child controls.

//The Page OnInit method is the only one
//of the main Page event methods that deviates from base Control format,
//and has some code in it worth knowing about...
////PsuedoCode:
protected internal override void OnInit(EventArgs e){
  //...falls back to default Control.OnInit,
  //which raises the Init event:
  base.OnInit(e);

  if (this._theme != null){
    this._theme.SetStyleSheet();
  }
  if (this._styleSheet != null){
    this._styleSheet.SetStyleSheet();
  }
}
			
1.1 Page
> > > > >Init
Raised after Control and all children have been initialized.
Is called prior to parent's OnInit.
LoadViewState not called yet: VState is not yet populated with previous request's vars.
Too early to refer to parent or any child controls. )
1.1 Page
> > > >TrackViewState
Starts tracking of VState changes to the control so they can be stored in the control's StateBag object.
Any changes made to VState of Control prior to this are not serialized.
LoadViewState not called yet: VState is not yet populated with previous request's vars.
2.0 Page
> > >InitComplete
Raised indicating that Initialization of Page and its Child Controls is done.
IMPORTANT:
It is important to note that on PostBack, the VState has not yet been restored.
1.1 Page
> > >LoadAllState
##########

//PsuedoCode:
private void LoadAllState(){
  this.LoadPageStateFromPersistenceMedium();
  //...then it's unclear in which order
  //it sets the controls...
  //then finally:
  base.LoadViewStateRecursive();
}

			
1.1 Page
> > > >LoadPageStateFrom PersistenceMedium
Loads saved VState to the Page object.
Override (and SaveStateToPersistenceMedium) to specify saving VState to other than hidden fields.
1.1 Page
> > > >LoadViewStateRecursive
Calls LoadViewState recursively(top down) on Page/all Controls.

//PsuedoCode:
internal virtual void LoadViewStateRecursive(object blob){
  ...parse blob...
  this.LoadViewState(partblob);
  foreach(Control childControl in this.Controls){
    ...parse blob...
    childControl.LoadViewStateRecursive(partblob);
  }
  this._controlState = ControlState.ViewStateLoaded;
  //Note: does not raise any other events?
}

1.1 Page
> > > > >LoadViewState
Restore VState from previous request, as saved by SaveViewState.
1.1 Control
> > > > >LoadViewState
description="Restore VState from previous request as saved by SaveViewState.

//PsuedoCode:
void LoadViewState(object savedState){
  if (savedState == null){return;}
  this.VState.LoadViewState(savedState);
  if (this.VState["Visible"] == null){return;}
}

1.1 [Sub]Control
> > > > >LoadViewState
##########
1.1 Page
> > >ProcessPostData[1]
Called after LoadViewState has completed, but before PreLoad.
Commonly referred to as 'ProcessPostData1', hands postback data to the controls that exist before OnLoad.

IMPORTANT:
Notice how on PostBack, this code uses FindControl -- and therefore triggers EnsureChildControls() earlier than the First Request's PreRender moment...




****************************
*** VERY IMPORTANT TO NOTICE
****************************
Notice that this function calls FindControl...
which kicks in and triggers EnsureChildControls().
****************************


//PsuedoCode:
private void ProcessPostData(NameValueCollection postData, bool fBeforeLoad){
  foreach (string text2 in postData){
    if ((text2 == null) || Page.IsSystemPostField(text2)){continue;}
    Control childControl = this.FindControl(text2);
    if (childControl == null){
    //Cute...when you think of it...
	  if (!fBeforeLoad){continue;}
      this._leftoverPostData.Add(text2, null);
      continue;
    }
    if ((childControl.PostBackDataHandler == null){
      this.RegisterRequiresRaiseEvent(childControl.PostBackEventHandler);
    }
    continue;
      IPostBackDataHandler handler1 = childControl.PostBackDataHandler;
    if ((handler1 != null) &&
    	handler1.LoadPostData(text2, this._requestValueCollection)){
      this._changedPostDataConsumers.Add(childControl);
    }
    if (this._controlsRequiringPostBack != null){
      this._controlsRequiringPostBack.Remove(text2);
    }
  }
}

1.1 Page
> > > >FindControl
##########
Ends up doing a recursion through all children until each namingcontainer's children/descendant controls have been created...
which of course is triggering EnsureChildControls.
1.1 Page
> > > >EnsureChildControls
Ensures that child Controls are created.
*** VERY IMPORTANT:
EnsureChildControls is called at this stage only on PostBack
On initial request it is deferred to PreRender stage...
2.0 Page
> > >OnPreLoad
##########
Available in Page only...
This method would probably be the right place to play around with objects before the universally used load event is fired.
2.0 Page
PreLoad
##########
See remarks for OnPreLoad.
1.1 Page
> > >LoadRecursive
Recursively (top down) calls OnLoad on all Controls.

//PsuedoCode:
internal virtual void LoadRecursive(){
  this.OnLoad(EventArgs.Empty);
  foreach(Control childControl in this.Controls){
    childControl.LoadRecursive();
  }
  this._controlState = ControlState.Loaded;
  //Note: does not raise any other operations?
}

1.1 Page
> > > >OnLoad
Called when control is loaded into Page. Raises Load event.
VState and other controls are now available.
IMPORTANT:
Parent OnLoad is called before child OnLoad -- reverse of OnInit.
1.1
> > > > >Load
Raised when control is loaded into the Page.
See remarks for OnLoad.
1.1 Control
> > > >OnLoad
Called when control is loaded into the Page. Raises Load event.
VState and other controls are now available.
*** Important:
parent OnLoad is called before child OnLoad -- reverse of OnInit.
1.1 Control
> > > > >Load
Raised when control is loaded into Page.
See remarks for OnLoad.
1.1 [Sub]Control
> > > >OnLoad
Called when the control is loaded into the Page object. Raises Load event.
VState and other controls are now available.
*** Important: parent OnLoad is called before child OnLoad -- reverse of OnInit.
1.1 [Sub]Control
> > > > >Load
See OnLoad.
1.1 Page
> > >ProcessPostData[2]
Called after Load and RaiseChangedEvents.
Commonly referred to as 'ProcessPostData2',
Hands postback data to dynamically added Controls that were added to the page control heirarchy in Load (UserControls loaded with LoadControl, etc.)
1.1 Page
> > >RaiseChangedEvents
##########
1.1 Page
> > >RaisePostBackEvent
##########
1.1 Control
> > > >OnClick
[OPTIONAL] Method called if a user has clicked the Control. Raises Click event.
1.1 Control
> > > > >Click
[OPTIONAL] Raises Click event.
See remarks for OnClick.
2.0 Page
> > >OnLoadComplete
Available in Page only.
Method called when all child controls of a page have been Loaded. Raises LoadComplete event.
2.0 Page
> > > >LoadComplete
See remarks for OnLoadComplete.
0.0 Page
Validate
##########
PreRender
1.1 Page
> > >PreRenderRecursiveInternal
Recursively (top down) calls OnPreRender of Page and all child controls.

//PsuedoCode:
internal virtual void PreRenderRecursiveInternal(){
  if (this.Visible){
    this.EnsureChildControls();
    this.OnPreRender(EventArgs.Empty);
    foreach(Control childControl in this.Controls){
      childControl.PreRenderRecursiveInternal();
    }
  }
  this._controlState = ControlState.PreRendered;
}

1.1 Page
> > > >EnsureChildControls
Ensures that child Controls are created.
IMPORTANT:
On Initial Request, since it did not EnsureChildControls earlier (it's important to understand that it never needed to as it wasn't processing events or postback data), it does it now.
1.1 Page
> > > >OnPreRender
Called when control is about to begin rendering. Raises PreRender event.
Use this event to perform VStateable updates. Changes made after this point will not be VStateable.
1.1 Page
> > > > >PreRender
Called when control is about to begin rendering.
See remarks for OnPreRender.
1.1 Control
> > > >EnsureChildControls
Ensures that child Controls are created.
On initial request, since it did not EnsureChildControls earlier (it's important to understand that it never needed to as it wasn't processing events or postback data), it does it now.
1.1 Control
> > > >OnPreRender
Called when control is about to begin rendering. Raises PreRender event.
Override to perform VStateable updates. Changes made after this point will not be VStateable.
1.1 Control
> > > > >PreRender
Called when control is about to begin rendering.
See remarks for OnPreRender.
1.1 [Sub]Control
> > > >EnsureChildControls
Ensures that child Controls are created.
On initial request, since it did not EnsureChildControls earlier (it's important to understand that it never needed to as it wasn't processing events or postback data), it does it now.
1.1 [Sub]Control
> > > >OnPreRender
Called when control is about to begin rendering. Raises PreRender event.
Override to perform VStateable updates. Changes made after this point will not be VStateable.
1.1 [Sub]Control
> > > > >PreRender
Called when control is about to begin rendering.
See remarks for OnPreRender.
1.1 Page
> > >PerformPreRenderComplete
Called when Page.PreRenderRecursiveInternal is complete