C# Development How To Notes
Understanding the order of Page events
- Understanding the lifecycle of a page
- Sequence of events for a Page
- Sequence of events for a Master Page
- Sequence of events for a User Control
- Further reading
Every ASP.NET page (be it C#, VB.NET or any other .NET derivative) when used has a simple lifecycle:
A request causes ASP.NET to create the page (or if it can, to instead use a cached response to avoid running the page).
The page is created, and page properties set (such as Request, Response and IsPostBack).
Controls on the page are assigned a unique ID.
Control properties are loaded. If this is a post-back then the properties are loaded with values in the viewstate or those posted back to the form.
- Postback event handling
If this is a post-back then any necessary event handlers for the controls on the form will be called.
View state information is saved, and the form contents rendered.
The page has been sent to the client. The Page.Response and Page.Request objects are no longer available. Any clean up is performed.
Start stage complete.|
protected void Page_PreInit(object sender, EventArgs e)
Session state available.
ViewState not not yet available. For postback, values of controls have not yet been restored from view state.
All controls initialised. The Init event of any controls are
triggered before the page's Init event. |
protected void Page_Init(object sender, EventArgs e)
Initialization stage complete.|
protected void Page_InitComplete(object sender, EventArgs e)
Page has loaded ViewState for all controls.|
protected void Page_PreLoad(object sender, EventArgs e)
Values of all controls have been loaded.|
protected void Page_Load(object sender, EventArgs e)
The page Load event is triggered before the Load event for any controls.
|(Control events - such as OnClick etc)|
All Load event handlers (for the page and any controls) have been
protected void Page_LoadComplete(object sender, EventArgs e)
Final event prior to rendering.|
protected void Page_PreRender(object sender, EventArgs e)
The Page's PreRender is raised prior the PreRender for any controls.
This is the last opportunity to do things which will affect the appearance of the form.
protected void Page_PreRenderComplete(object sender, EventArgs e)
ViewState saved for the page and all controls.|
protected void Page_SaveStateComplete(object sender, EventArgs e)
This is a processing stage rather than an event. The default
method will render the page - i.e. generate the necessary HTML/XHTML
for the page and its controls. The default method calls the render
method for each control.|
protected override void Render(HtmlTextWriter writer)
If you create this function then you become responsible for rendering of the form. If you create the function and leave its body empty then you will get an empty web page.
Final opportunity to perform any necessary clean-up or post
rendering activities for the page.|
protected void Page_Unload(object sender, EventArgs e)Unload is triggered for each control and then for the page.
A master page does not have the same number of events triggered as a normal page. Only the following events are triggered, and in this order:
Sometimes it can be important to understand the sequencing of these events. The following shows explicitly the sequence in which events are triggered for a page and for the master page it uses:
Each control responds to a number of events. The events that a control can respond to are a subset of the number of events which a page can respond to. Only the following events are triggered, and in this order:
In order to understand the sequencing of these events, the following table shows explicitly the sequence in which events are triggered for a control and the page it is on:
|(Control events - such as OnClick etc)|
- ASP.NET Page Life Cycle Overview - msdn article from Microsoft.
These notes have been tested within Microsoft Visual Studio 2010 and Visual Studio 2008, and may apply to other versions as well.
About the author: Brian Cryer is a dedicated software developer and webmaster. For his day job he develops websites and desktop applications as well as providing IT services. He moonlights as a technical author and consultant.