SPS Home > Dgreath > ASPX Page Lifecycle |
ASPX Page Life CycleEvery 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 StartNote: These delegates are located in the global.asax file.
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.
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
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
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
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
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
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
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
Triggered By: all control events have been completed. Use: to trigger anything that can't be done until all controls have been loaded. |
PreRender
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
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
Triggered By: completion of the page render process. Use: final cleanup—disconnect databases, third party services, etc. |
Error
Triggered By: unhandled exceptions from this page's try-catch blocks bubbling up. Use: Handle page level exceptions. |
Global EndNote: These delegates are located in the global.asax file.
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.
Triggered By: application closing down. Use: release custom application resources.
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. |