Cry about...
C# Development How To Notes

Understanding the order of Page events


Contents:

Understanding the lifecycle of a page

Every ASP.NET page (be it C#, VB.NET or any other .NET derivative) when used has a simple lifecycle:

  1. Request

    A request causes ASP.NET to create the page (or if it can, to instead use a cached response to avoid running the page).

  2. Start

    The page is created, and page properties set (such as Request, Response and IsPostBack).

  3. Initialization

    Controls on the page are assigned a unique ID.

  4. Load

    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.

  5. Postback event handling

    If this is a post-back then any necessary event handlers for the controls on the form will be called.

  6. Rendering

    View state information is saved, and the form contents rendered.

  7. Unload

    The page has been sent to the client. The Page.Response and Page.Request objects are no longer available. Any clean up is performed.

Sequence of events for a Page

Page EventDescription
PreInit Start stage complete.

protected void Page_PreInit(object sender, EventArgs e)

IsPostBack available.

Session state available.

ViewState not not yet available. For postback, values of controls have not yet been restored from view state.

Init 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)

InitComplete Initialization stage complete.

protected void Page_InitComplete(object sender, EventArgs e)

PreLoad Page has loaded ViewState for all controls.

protected void Page_PreLoad(object sender, EventArgs e)

Load 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)
LoadComplete All Load event handlers (for the page and any controls) have been raised.

protected void Page_LoadComplete(object sender, EventArgs e)

PreRender 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.

PreRenderComplete

protected void Page_PreRenderComplete(object sender, EventArgs e)

SaveStateComplete ViewState saved for the page and all controls.

protected void Page_SaveStateComplete(object sender, EventArgs e)

Render 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.

Unload 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.

Sequence of events for a Master 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:

  1. Page_Init
  2. Page_Load
  3. Page_PreRender
  4. Render
  5. Page_Unload

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:

Master pagePage
-Page.Page_PreInit
MasterPage.Page_Init-
-Page.Page_Init
-Page.Page_InitComplete
-Page.Page_PreLoad
-Page.Page_Load
MasterPage.Page_Load-
-Page.Page_LoadComplete
-Page.Page_PreRender
MasterPage.Page_PreRender-
-Page.Page_SaveStateComplete
-Page.Render
MasterPage.Render-
MasterPage.Page_Unload-
-Page.Page_Unload

Sequence of events for a User Control

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:

  1. Page_Init
  2. Page_Load
  3. Page_PreRender
  4. Render
  5. Page_Unload

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:

PageUser Control
Page.Page_PreInit-
-UserControl.Page_Init
Page.Page_Init-
Page.Page_InitComplete-
Page.Page_PreLoad-
Page.Page_Load-
-UserControl.Page_Load
(Control events - such as OnClick etc)
Page.Page_LoadComplete-
Page.Page_PreRender-
-UserControl.Page_PreRender
Page.Page_SaveStateComplete-
Page.Render-
-UserControl.Render
-UserControl.Page_Unload
Page.Page_Unload-

Further Reading


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