Sunday, 20 May 2012

page life cycle in asp.net


Page events in sequence

1.OnPreInit
2.OnInit
3.OnInitComplete
4.OnPreLoad
4.page_load
5.OnLoad
6.OnLoadComplete
7.OnPreRender
8.OnPreRenderComplete
9.OnSaveStateComplete
10.Render
11.OnUnload
Example



    protected override void OnSaveStateComplete(EventArgs e)
    {
        base.OnSaveStateComplete(e);
        Response.Write("<br/>OnSaveStateComplete");
    }
    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        Response.Write("<br/>LoadViewState");
    }
    protected override void  OnError(EventArgs e)
    {
      base.OnError(e);
         Response.Write("<br/>OnError");
    }
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        Response.Write("<br/>OnPreRender");
    }
    protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);
        Response.Write("<br/>OnPreInit");
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        Response.Write("<br/>OnInit");
    }
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        Response.Write("<br/>OnLoad");
    }
    protected override void OnLoadComplete(EventArgs e)
    {
        base.OnLoadComplete(e);
        Response.Write("<br/>OnLoadComplete");
    }

    protected override void OnInitComplete(EventArgs e)
    {
        base.OnInitComplete(e);
        Response.Write("<br/>OnInitComplete");
    }
    protected override void OnPreLoad(EventArgs e)
    {
        base.OnPreLoad(e);
        Response.Write("<br/>OnPreLoad");
    }
    protected override void OnPreRenderComplete(EventArgs e)
    {
        base.OnPreRenderComplete(e);
        Response.Write("<br/>OnPreRenderComplete");
    }
    protected override void OnUnload(EventArgs e)
    {
        base.OnUnload(e);
        //Response.Write("<br/>OnUnload");
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("<br/>page_load");
    }

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



enable trace in aspx page
<%@ Page Language="C#"  Trace="true" Async="false" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

output would be same as shown above