Jitesh Byahut 8/29/2014 3121

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.

1. Set master page dynamically

2. Set theame dynamically values

3. Read or set profile property

4. Create or recreate dynamic controls

5. 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

1. Use this event to read or initialize control properties.

2. Dynamically add controls to the page

2. 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.

1. It's load ViewState for page and all controls.

2. 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.

1. Set properties of controls

2. Connect database

protected void Page_LoadComplete(object sender, EventArgs e)

{

     

}

Control Event

Control events occures before Page_LoadComplete event.

1. 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

1. Use this event to do final cleanup for specific control such as closing control specific database connections.

2. The page and its controls have been rendered, so you cannot make further changes to the response stream.

3. 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)

{

      

}


Please give your feedback for improving this page