ASP.NET Page Lifecycle


Sponsored Links

73058_New Scooba® 230 Floor Washing Robot + Free Shipping!

 

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

Init

InitComplete

PreLoad

Load

Control events eg Button Click

LoadComplete

PreRender

PreRenderComplete

SaveStateComplete

Render (method not an event)

Unload

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.