Sunday, 31 May 2020

Page Life Cycle of Asp.Net



Page Request
The page request occurs before the page life cycle begins, i.e. when the page is requested by a user. It determines whether the page needs to be parsed and compiled (therefore beginning the life of a page) or whether a cached version of the page can be sent in response without running the page.


Stage1: Start
In this stage the page properties such as request and response are created and set, and at this stage the page also determines whether the request is a postback or a new request and sets the IsPostBack property, which will be true if it’s a PostBack or else false if it’s a first request.

Stage2: Initialization
During this stage all the controls on the page will be created and each controls unique Id property will be set.
Stage3: Load
In this stage if the current request is a postback, control properties are loaded with information retrieved from view state.

Stage4: Validation 
In this stage any server side validations that are required will be performed and sets the IsValid property of individual validator controls and then of the page.

Stage5: Postback Event Handling
If the request is a posback then all the controls corresponding event procedures are called (which is required only) along with any cached events also.

Stage6: Rendering
In this stage the page is rendered i.e. converts into html format and before rendering view state and control state is saved for the page and all controls.
Note:During the rendering stage, page calls the render method for each control and writes its output to the OutputStream object of the page’s Response property.

Stage7: UnLoad:
After the page has been rendered and the output is sent to the client,it’s ready to discard and Unload event is raised,and at this stage the Request and Response properties are destroyed and cleanup is performed.
Page Events:
Within each stage of the page life cycle, the page raises certain events which can be used for performing different actions. List of page Life Cycle events that we will use most frequently:

1.  PreInit
2.  Init
3.  InitComplete
4.  PreLoad
5.  Load
-Control Events (All Postbackand Cached Events)
6.  LoadComplete
7.  PreRender
8.  PreRenderComplete
9.  SaveStateComplete
 -RenderMethod Calling
10.    UnLoad

1.PreInit: Raised after the start stage is complete and before the initialization stage begins. We use this event forperforming the following action:
  •   Check the IsPostBack property to determine whether this is the first time page is being         processed.   
  • The IsCallback and IsCrossPagePostBack properties are also set at this time.
  •   Create or re-create dynamic controls.
  •  Set a master page dynamically.
  • Set the Theme property dynamically.


2.Init:  Raised after all controls have been initialized and any skin settings have been applied. The Init event of individual controls occurs before the Init event of the page. Use this event to read or initialize control properties.

3. InitComplete: Raised at the end of page's initialization stage. Only one operation takes place between the Init and InitComplete events i.e. tracking of view state is turned on. View state tracking enables controls to persist any values that are programmatically added to the ViewState collection. Until view state tracking is turned on, any values added to view state are lost across postbacks. Controls typically turn on view state tracking immediately after they raise their Init event. Use this event to make changes to view state that you want to make sure are persisted after the next postback.

4. PreLoad: Raised after the page loads view state for itself and all controls, and after it starts processingpostback data that is included with the Request object.

5. Load: The Page object calls the OnLoad method on the Page object, and then recursively does the same for each child control until all controls are loaded. The Load event of individual controls occurs after the Load event of page. Use this event to get or set properties to controls and to establish database connections.

Control events: Use these events to handle specific control events, such as a Button control's Click event or a TextBox control's TextChanged event.In a postback request, if the page contains validation controls, check the IsValid property of the Page before performing any processing.

6. LoadComplete: Raised at the end of event-handling stage. Use this event for tasks that require for all other controls on the page been loaded.

7. PreRender: Raised after the Page object has created all controls that are required in order to render the page, including child controls of composite controls.The Page object raises the PreRender event on itself and then recursively does the same for each child control. Use this event to make final changes to contents of the page or its controlsbefore the rendering stage begins.

8. PreRenderComplete: Raised after the controls on the page are ready for render.

9. SaveStateComplete: Raised after view state and control state have been saved for the page and for all controls. Any changes to the page or controls at this point affect rendering, will not be retrieved on the next postback.

Render: This is not an event; at this stage of processing, the Page object calls this method on each control.

Note:All ASP.NET Web server controls have a RenderControl method that writes out the control's markup that has to be sent to the browser as output.

10. Unload: Raised for each control and then for the page. Use this event to do final cleanup for specific controls, such as closing control-specific database connections.

Note:During the unload stage, the page and its controls rendering is completed, so you cannot make further changes to the response stream. If you attempt to call a method such as the Response.Write, page will throw an exception.

AutoEventWireUp:Pages also support automatic event wire-up, meaning that ASP.Net looks for methods with particular names and automatically runs those methods when certain events are raised. If the AutoEventWireup attribute of the @Page directive is set to true, page events are automatically bound to methods that use the naming convention of Page_Event such as Page_Load,Page_PreInit, Page_Unloadetc.



Posted By:  pankaj_bhakre

No comments:

Post a Comment