ASP.NET Web Page Life Cycle Events

A web page has a set of events that get raised at during the ASP.NET Page Life Cycle. ASP.NET looks for methods with certain names and automatically runs those methods when certain events are raised.

In this example we will show each event being used and show the order in which they get raised


namespace WebPageLifeCycleEventsTestApp
{
    public partial class _Default : Page
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            Debug.WriteLine("PreInit_Load");
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            Debug.WriteLine("Init_Load");
        }

        protected void Page_InitComplete(object sender, EventArgs e)
        {
            Debug.WriteLine("InitComplete_Load");
        }

        protected void Page_PreLoad(object sender, EventArgs e)
        {
            Debug.WriteLine("PreLoad_Load");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Debug.WriteLine("Page_Load");
        }

        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            Debug.WriteLine("LoadComplete_Load");
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            Debug.WriteLine("PreRender_Load");
        }

        protected void Page_PreRenderComplete(object sender, EventArgs e)
        {
            Debug.WriteLine("PreRenderComplete_Load");
        }

        protected void Page_SaveStateComplete(object sender, EventArgs e)
        {
            Debug.WriteLine("SaveStateComplete_Load");
        }

        protected override void Render(HtmlTextWriter output)
        {
            Debug.WriteLine("Render");
            base.Render(output);
        }

        protected void Page_Unload(object sender, EventArgs e)
        {
            Debug.WriteLine("Unload_Load");
        }
    }
}

Hint: As Visual Studio does not help C# developers create these methods, it is handy to have them available to copy and paste.

This is what is displayed in Visual Studio’s Output window


PreInit_Load
Init_Load
InitComplete_Load
PreLoad_Load
Page_Load
LoadComplete_Load
PreRender_Load
PreRenderComplete_Load
SaveStateComplete_Load
Render
Unload_Load



Comments

No comments yet.

Add Yours

  • Author Avatar

    YOU


Comment Arrow




About Author

Robert

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning hands down.