SPS Home    >   Dgreath    >   ASPX Page Lifecycle

ASPX Page Life Cycle

Every aspx page (which, of course, is an instance of the Page class) has a set of published events that are fired sequentially as the process develops. Delegates can be subscribed via the built-in "wireup" mechanism into each of these events as needed. For the automatic event wireup to work, these must follow an exact signature as shown below. The following signatures illustrate each of these delegates along with some notes about what has happened behind the scenes and what each should be used for. Global start and end delegates are located in the global.aspx file and occur before and after the page lifecycle but are a factor in every page process. Unless otherwise noted, the following delegates are located on each webform's code-behind page. 

Global Start

Note: These delegates are located in the global.asax file.

protected void Application_Start(object sender, EventArgs e)
{
   //Implementation code goes here:
}

Triggered By: application start up.

Use: create and initialize custom application resources for subsequent use.  Application is useful to initialize values that are common to all pages.


protected void Session_Start(object sender, EventArgs e)
{
   //Implementation code goes here:
}

Triggered By: creation of session.

Use: any task required by session creation for subsequent use. Session is useful for passing values between pages. (for security reasons, avoid passing data in querystrings). Session can be read or written at anytime in the page lifecycle, however, due to the event driven nature of webforms, changes may not be seen until the next postback.

Page Start...

Notes: Request and Response objects are created.   IsPostBack status is determined.

Pre Initialization

protected void Page_PreInit(object sender, EventArgs e)
{
   //Implementation code goes here:
}

Triggered By:  IsPostBack status is determined.

Use:  create or re-create dynamic controls, or set masterpage dynamically, or set theme dynamically.

Note: Since viewstate has not yet been loaded, control state can subsequently change during processing of postback.

Initialization

protected void Page_Init(object sender, EventArgs e)
{
   //Implementation code goes here:
}

Triggered By: completion of initialization of every control from bottom to top, reserving the page to the last.

Use: to read or initialze control properties. Use to subscribe delegates to events. Dynamic controls can be created here if they are data dependent although PreInit is preferred for creation and Init for configuration.

Initialization Complete

protected void Page_InitComplete(object sender, EventArgs e)
{
   //Implementation code goes here:
}

Triggered By:  all initialization tasks are complete.

Use: to perform any task that requires all intitialization tasks complete. Typically this will involve changes to viewstate that need to be persisted.

Viewstate & Postback Data Loads...

PreLoad

protected override void OnPreLoad(EventArgs e)
{
   //Implementation code goes here:
}

Raised when viewstate is fully loaded and all postback data included with the request is available.

Use: process data from viewstate and passed via the postback request.

Declarative Control Creation Occurs...

Load

protected void Page_Load(object sender, EventArgs e)
{
   //Implementation code goes here:
}

Triggered By: all controls have been fully created and are ready to accept data.

Note: "Load" refers to bringing in unique data to the page being rendered. Prior to this point, the page being built would be exactly the same in all instances—this is where the page becomes unique.

Use: to establish database connections and connections to third party services. Also used for dynamic control creation which are independent from data.

Control Events Processed

protected void SomeControl_SomeEvent(object sender, EventArgs e)
{
   //Implementation code goes here:
}

Triggered By: any control rasing an event via postback.

Note: these are all the control delegates for both dynamic and declarative controls. These events will only occur during postbacks.

Use: process any control related event.

LoadComplete

protected void Page_LoadComplete(object sender, EventArgs e)
{
   //Implementation code goes here:
}

Triggered By: all control events have been completed.

Use: to trigger anything that can't be done until all controls have been loaded.

PreRender

protected override void OnPreRender(EventArgs e)
{
   //Implementation code goes here:
}

Raised when all page controls needed to render the page have been created, but not yet sent to the stream.

Use: this is the last chance for any final adjustments to controls about to be rendered.

Viewstate & Controlstate Saved...

Save State Complete

protected override void OnSaveStateComplete(EventArgs e)
{
   //Implementation code goes here:
}

Raised after viewstate and controlstate have been saved for all controls and the page itself.

Note: at this point no further changes can be made to controls.

Use: for final updates to viewstate that do not involve controls. Viewstate is useful to pass values from page creation to all postback instances or between postbacks of this page.

Page is Rendered...

This  marks the point where page and its included controls and viewstate are rendered via the response stream to the requesting user agent behind the scenes.

Unload

protected void Page_UnLoad(object sender, EventArgs e)
{
   //Implementation code goes here:
}

Triggered By: completion of the page render process.

Use: final cleanup—disconnect databases, third party services, etc.

Error

protected void Page_Error(object sender, EventArgs e)
{
   //Implementation code goes here:
}

Triggered By: unhandled exceptions from this page's try-catch blocks bubbling up.

Use: Handle page level exceptions.

Global End

Note: These delegates are located in the global.asax file.

protected void Session_End(object sender, EventArgs e)
{
   //Implementation code goes here:
}

Triggered By: user abandons page explicitly or on session timeout (implicit abandonment) or programatically by a call to Session.Abandon.

Use: any task required by session abandonment.


protected void Application_End(object sender, EventArgs e)
{
   //Implementation code goes here:
}

Triggered By: application closing down.

Use: release custom application resources.


protected void Application_Error(object sender, EventArgs e)
{
   //Implementation code goes here:
}

Triggered By: unhandled page execeptions bubbling up.

Use: handle exceptions not caught at the page level.

Note: These delegates are located in the global.asax file.