Friday, July 6, 2012

ASP.NET Page Life Cycle Events


When a page request is sent to the Web server, the page is run through a series of events during its creation and disposal. In this article, I will discuss in detail the ASP.NET page life cycle Events 

(1) PreInit
(2)Init
(3)InitComplete 
(4)PreLoad
(5)Load
(6)Control (PostBack) event(s)
(7)LoadComplete
(8)PreRender
(9)SaveStateComplete
(10)Render
(11)UnLoad

(1) PreInit

    This is the very first page event that gets fired when a request for a page occurs.We can say "PreInit" as the entry point to the Life cycle events.At this satge we can dynamically set the master page,  set their skin properties and can access the controls in the master page as well.
   
From  the code block ,we can see how the master page has been set at runtime and how i accessed the controls in the master page dynamically.If u try to set the master page in any othe page events it will though error.

(2) Init

  This event fires after each controls has been initialized,their UniqueId has been set and skin settings have been applied .This event gets fired in the revers heirarchy order ,ie starting from child control to the parent ones untill its fired for the page itself.
We can use this method to change the initilization values of the control.

(3) InitComplete
 This event is rised once initialization of all controls and page completed. From this state onwards the ViewState for server controls become accessible.

(4) PreLoad
    This is the first event where viewstate functionality starts retrieving their values.In this method, page will load values from viewstate.

(5)PageLoad
     The page Load events executes after the Page_PreLoad event.By now the page has restored to its initial state in case of postback.Code inside the page load event typically checks for PostBack and then sets control properties appropriately. 



 (6)Control (PostBack) event(s)

   ASP.NET now calls any events on the page or its controls that caused the PostBack to occur. This might be a button’s click event or a dropdown's selectedindexchange event, for example.These are the events, the code for which is written in your code-behind class(.cs file). 


      
 

(7)LoadComplete

  This event signals the end of Load.We can use this event for the tasks which require all the controls in the page to be loaded.

(8)PreRender

  This event gets fired when page had loaded all the controls that user could see in his browser.This is the last event that could reflect any changes in the page ,that has to be shown in the browser.The PreRender event occurs for each controls in the page.

(9)SaveStateComplete

  Prior to this event ,ViewState have been loaded for page and for all the controls.Any changes to the page or controls at this stage will be ignored.We can use this event for tasks that require all ViewSate valuesto be saved,but that do not make any changes to the controls.

(10) Render

 The rendor method generates the client side HTML,DHTML and scripts that is required to render the output in the browser and to implement the client side functionalities.


(11) Unload

  This is the last event that gets fired.At this stage you can dispose any object and references that has been made to construct the page including the page object.
 


No comments:

Post a Comment