Page Event Life Cycle in Asp.Net - ProgramIdea

Page Event Life Cycle in Asp.Net

PreInit

PreInit is the first event in Asp.Net Page event life cycle. Its also known as pre initialization event. No ViewState availble at this stage.

  • Set master page dynamically
  • Set theame dynamically values
  • Read or set profile property
  • Create or recreate dynamic controls
  • Check the IsPostBack property to determine whether this page is the first time being processed

protected void Page_PreInit(object sender, EventArgs e)

{

    // Add master page dynamically

    this.MasterPageFile = "Master/GridView.master";

 

    // Add theme dynamically        

}

Init

Raised after all controls have been initializaed and any skin setting have been applied

  • Use this event to read or initialize control properties.
  • Dynamically add controls to the page.
  • Viewtate information is not available at this stage.

protected void Page_Init(object sender, EventArgs e)

{

      

}

InitComplete

  • Raised by the page object.
  • Use this event for processing task require all intialization be complete.

protected void Page_InitComplete(object sender, EventArgs e)

{

     

}

PreLoad

PreLoad event fires before ViewState has been loaded for the page and its controls and before postback processing. PreLoad event occures when all controls are initialized. Use this event, if you need to perform processing on your page or control before the load event.

  • It's load ViewState for page and all controls.
  • You can check IsPostBack

protected void Page_PreLoad(object sender, EventArgs e)

{

        

}

Here you can see ViewState available only from Page_PreLoad event.

Demo:

demo

Load

Page_Load event used by every programmer.

  • Set properties of controls
  • Connect database

protected void Page_LoadComplete(object sender, EventArgs e)

{

     

}

Control Event

  • Control events occures before Page_LoadComplete event.
  • Use this event to handle specific control events such as Button's Click event or DropDownList's Selected event.

protected void Button1_Click(object sender, EventArgs e)

{

       

}

Load Complete

  • Use this event for task that require that all other controls on page be loaded.
  • Occures when the server control is loaded into the page object.

protected void Page_LoadComplete(object sender, EventArgs e)

{

       

}

PreRender

  • This event allows final changes of controls or the page. it takes place after all regular postback event have taken place and before ViewState is saved, so any changed at this stage are saved.
  • Each data bound control whose DataSourceID property is set calls its DataBind method.
  • The PreRender event occurs for each control on the page. Use this event to make final changes to the controls of the page or its controls.

protected void Page_PreRender(object sender, EventArgs e)

{

        

}

SaveStateComplete

  • Before this event occures, ViewState has been saved for the page and for all controls
  • Any changes to the page or controls at this point will be ignored.
  • Use this event to perform task that require ViewState to be saved, but that do not any changes to controls.

protected void Page_SaveStateComplete(object sender, EventArgs e)

{

 

}

UnLoad

  • Use this event to do final cleanup for specific control such as closing control specific database connections.
  • The page and its controls have been rendered, so you cannot make further changes to the response stream.
  • If you attemp to call a method such as the Response.Write method, then the page will throw an exception.

protected void Page_UnLoad(object sender, EventArgs e)

{

      

}