Tuesday, 28 May 2013

ASP.NET Page Life Cycle Events

ASP.NET Page Life Cycle Events

 The page life cycle events are :

                    

PreInit 


Init


InitComplete

PreLoad


Load


ControlEvents


LoadComplete

PreRender


PreRenderComplete


SaveStateComplete


RenderComplete

Unload



PreInit  : 

This is the initial stage of the page means this is entry point  of page life cycle .
This  checks for is PostBack that means  first time the request is sent or the first time page is processed. Dynamic controls are created in this event such as:
To Set master page dynamically.
To Set the Theme property dynamically,
To Read or set profile property values.


Init:
In this Init Event for the individual control is initialised first  and later the init for the page takes place. This  event is used to initialize the control properties i.e. Each control in the control collection is initialized.

InitComplete:
 View State is turned on in this event . Any changes made to the View State in this event are persisted even after the next postback. In simple sense page is initialised  and  process completes. 

PreLoad:
This event is called before the loading of the page is completed.


Load:
This Load means that all the controls have been loaded completely .  This implies that the  control properties and the view state can be accessed at this point/cycle.
This works like initially ,   the Page Object calls the OnLoad method on the Page object itself  and  then OnLoad method of the control is called .  This means that the Load event of Page occurs before Load event of the controls . 

ControlEvents:
This event manages the events for the buttons , textboxes etc  Like button click event , textbox : textchange event .
In the case of postback , the Page.Isvalid property checks  if the page contains validator controls . 

LoadComplete :
This means that the page has been loaded in the memory. It also marks the beginning of the rendering stage and occurs after the event handling stage. This event is used for tasks such as loading all other controls on the page.

PreRender:
If you need to make any final changes /modification  to the contents of the controls or the page, then we use this event. In this event the PreRender event of the page is called first and later for the child control.

    For eg : To set  the DataSourceId Property  and calling the DataBind method.

PreRenderComplete:
This event is raised after each control's PreRender property/phase  is completed.

SaveStateComplete:
In this event, the current state of the control is completely saved to the ViewState.This is raised after the control state and view state have been saved for the page and for all controls.


RenderComplete:
     The page object calls this method on each control which is present on the page.   This method writes the control’s markup to send it to the browser. 

Unload:
This event is typically used for closing files and database

 connections.