ASP.NET Page Lifecycle


InterServer Web Hosting and VPS

ASP.NET Page Lifecycle

I wanted to help myself get to grips with the ASP.NET Page Life Cycle so I've produced a summary here.

ASP.NET Page LifeCycle Summary

Here is a summary of the events (and method) which get fired during the processing of the ASP.NET Page Life Cycle as of .NET 4.0.


PreInit
Init
InitComplete
PreLoad
Load
Control events
LoadComplete
PreRender
PreRenderComplete
SaveStateComplete
Render
Unload


PreInit

  • Create dynamic controls
  • Set Master Page dynamically
  • Set Theme dynamically
  • Set/Get Profile properties

Init

  • Control Init fires before Page Init
  • Set/Get Control properties
  • Don't access other controls

InitComplete

  • ViewState tracking switched on
  • Control(s) are accessible but won't have their user data set yet

PreLoad

  • Raised after ViewState has been loaded for Page and Control(s)
  • Raised after processing postback data in Request

Load

  • Load called for Page and then the child Controls recursively
  • Set Control properties and database connections
  • ViewState and POST data available
  • Other Control(s) in Page hierarchy are accessible

Control events eg Button Click

  • Check Page.IsValid where required in code

LoadComplete

  • All controls loaded
  • Postback data and ViewState has been loaded into Page and Control(s)

PreRender

  • PreRender called for Page and then child Control(s) recursively
  • Final changes to Page and Controls before rendering begins

PreRenderComplete

  • Raised after each Control's DataBind is called, which is called when DataSourceID is set
  • Last event before ViewState is saved

SaveStateComplete

  • Raised after ViewState and Control state saved for Page and Control(s)
  • Changes here affect this Page rendering but will not be persisted on a Postback

Render (method not an event)

  • Method not an event
  • Page calls Render on each Control
  • Writes out markup for browser
  • Override in a custom control for custom markup

Unload

  • Cleanup code (such as closing database connections and closing files)
  • Page and Control(s) already rendered so can't be changed

ASP.NET Page LifeCycle Example

Here is a simple example which overrides each of the event handlers for these events and the Render method, as this is not an event. Notice the inclusion of a button onclick event handler which isn't currently utilised. The following shows the HTML output:


OnPreInit
OnInit
OnInitComplete
OnPreLoad
OnLoad
OnLoadComplete
OnPreRender
OnPreRenderComplete
OnSaveStateComplete
Render



using System;
using System.Web.UI;

namespace PageLifeCycle
{
    public partial class _Default : System.Web.UI.Page
    {
        protected override void OnPreInit(EventArgs e)
        {
            Response.Write("OnPreInit <br />");
            base.OnPreInit(e);
        }

        protected override void OnInit(EventArgs e)
        {
            Response.Write("OnInit <br />");
            base.OnInit(e);
        }

        protected override void OnInitComplete(EventArgs e)
        {
            Response.Write("OnInitComplete <br />");
            base.OnInitComplete(e);
        }

        protected override void OnPreLoad(EventArgs e)
        {
            Response.Write("OnPreLoad <br />");
            base.OnPreLoad(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            Response.Write("OnLoad <br />");
            base.OnLoad(e);
        }

        protected void Button_Postback_Click(object sender, EventArgs e)
        {
            Response.Write("Button OnClick on Postback <br />");
        }

        protected override void OnLoadComplete(EventArgs e)
        {
            Response.Write("OnLoadComplete <br />");
            base.OnLoadComplete(e);
        }

        protected override void OnPreRender(EventArgs e)
        {
            Response.Write("OnPreRender <br />");
            base.OnPreRender(e);
        }

        protected override void OnPreRenderComplete(EventArgs e)
        {
            Response.Write("OnPreRenderComplete <br />");
            base.OnPreRenderComplete(e);
        }

        protected override void OnSaveStateComplete(EventArgs e)
        {
            Response.Write("OnSaveStateComplete <br />");
            base.OnSaveStateComplete(e);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            Response.Write("Render <br />");
            base.Render(writer);
        }

        protected override void OnUnload(EventArgs e)
        {
            // Response.Write("OnUnload <br />");
            // generates Exception:
            // Response is not available in this context.
            base.OnUnload(e);
        }
    }
}


ASP.NET Page LifeCycle Example with a Postback

This example uses the same code as above but is the result of a postback, caused by the Click event of a button:


OnPreInit
OnInit
OnInitComplete
OnPreLoad
OnLoad
Button OnClick on Postback
OnLoadComplete
OnPreRender
OnPreRenderComplete
OnSaveStateComplete
Render


References

Of course I'll have to thank Microsoft for putting this reference information online at http://msdn.microsoft.com/en-us/library/ms178472.aspx.

Sponsored Links
SolarSystem - AngularJS Demo
SolarSystem - Angular JS Demo

Check out my first Angular JS demo hosted in Azure, it helped win my first Angular contract. Put the sound on.