Sunday, 13 September 2015

Page life cycle of Asp.net





Following are steps involves in the Asp.net page life cycle.
  • S – Start
  • I – Initialize
  • L – Load
  • V – Validate
  • E – Event Handling
  • R – Render

1. Page Request :When user requests for the page, ASP.NET checks if it is a first request then page needs to be parsed and compiled and if it is not a first request then the page can be sent from the cached version store on the server.

2. Start : In this Request, Request properties are set and if it is a first request then Request, Response, IsPostBack and UICulture are set also set. If we need to access or override behavior for this step, use the PreInit method to create or re-create dynamic controls, set a master page or theme or read or set profile property values

3. Page Initialization : Each control uniqueId is created .
Three Page initialization events are : 
  • Init : After initialization of all controls this event is called and it is also used to initialize control properties.
  • InitComplete :This event is raised by the page object and is used for processing tasks which are required for initialization.
  • PreLoad :  Use this event if you need to perform processing on your page or control before the Load event. After the Page raises this event, it loads view state for itself and all controls, and then processes any post-back data included with the Request instance. 

4. Load : In this stage controls are loaded with information retrieved from view and
control states. This is where we will want to set properties for all of the server controls
on our page, request query string and establish database connections.

5. Validation : If we have controls that require validation, they are validated here and
we can check the IsValid property of the control.

6. Event Handling : The event handling for server controls occurs during this stage. This means that events such as click, selectedIndexChanged etc are applied to your sever controls, and in the case of postback, these event handlers are fired by the control. The accessible events of note in this
stage age as follows :
  • Load Complete : All of the controls for the page have been loaded.
  • PreRender : A few things of import happen here. First, the page object will call "EnsureChildControls" for each control,and finally for the page. Additionally any data bound control that has a "DataSourceId" set will call its "DataBind" method. It is important to note that the "PreRender" event occurs for each control on the page. At the conclusion of the event, viewstate will be saved for the page and all of the controls.
  • SaveStateComplete : ViewState has been saved. If you have actions that do not require changes to controls but require Viewstate to have been saved, you can handle the "SaveStateComplete" event.

7.  Render : Render is not really an event. Rather, the page object calls this method on each control, Which in turn writes out the HTML markup for the control to the browser.

8. Unload : This final event occurs first for each control, then, finally, for the page. At this point, all controls have been rendered to the output stream and cannot be changed. During this event any attempt to access the response stream will result in an exception being thrown.




1 comment: