Search This Blog

2010/12/15

ASP.NET Page Life Cycle


      When an ASP.NET page runs, the page goes through a life cycle in which it performs a series of processing steps. It is important to understand the page life cycle so that we will able to write code at the appropriate life-cycle stage for the effect we intended to be.
The page goes through the stages outlined in the following table. Some parts of the life cycle occur only when a page is processed as a postback. Page life cycle is the same during a partial-page postback as it is during a full-page postback.
















Stage
Description
Page request
The page request occurs before the page life cycle begins. When the page is requested by a user, ASP.NET 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.
Start
In the start stage, page properties such as Request and Response are set. At this stage, the page also determines whether the request is a postback or a new request and sets the IsPostBack property. The page also sets the UICulture property.
Initialization
During page initialization, controls on the page are available and each control's UniqueID property is set. A master page and themes are also applied to the page if applicable. If the current request is a postback, the postback data has not yet been loaded and control property values have not been restored to the values from view state.
Load
During load, if the current request is a postback, control properties are loaded with information recovered from view state and control state.
Postback event handling
If the request is a postback, control event handlers are called. After that, the Validate method of all validator controls is called, which sets the IsValid property of individual validator controls and of the page.
Rendering
Before rendering, view state is saved for the page and all controls. During the rendering stage, the page calls the Render method for each control, providing a text writer that writes its output to the OutputStream object of the page's Response property.
Unload
The Unload event is raised after the page has been fully rendered, sent to the client, and is ready to be discarded. At this point, page properties such as Response and Request are unloaded and cleanup is performed.

Life-Cycle Events

      Within each stage of the life cycle of a page, the page raises events that one can handle to run his own code. 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 and Page_Init.

The following table lists the page life-cycle events that are used most frequently. There are more events than those listed; however, they are not used for most page-processing scenarios. Instead, they are primarily used by server controls on the ASP.NET Web page to initialize and render themselves.
Page Event Typical Use
PreInit Raised after the start stage is complete and before the initialization stage begins.
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.
InitComplete Raised at the end of the page's initialization stage. Only one operation takes place between the Init and InitComplete events: tracking of view state changes 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.
PreLoad Raised after the page loads view state for itself and all controls, and after it processes postback data that is included with the Request instance.
Load The Page object calls the OnLoad method on the Page object, and then recursively does the same for each child control until the page and all controls are loaded. The Load event of individual controls occurs after the Load event of the page.
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.
LoadComplete Raised at the end of the event-handling stage.
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. (To do this, the Page object calls EnsureChildControls for each control and for the page.)
PreRenderComplete Raised after each data bound control whose DataSourceID property is set calls its DataBind method. For more information, see Data Binding Events for Data-Bound Controls later in this topic.
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, but the changes will not be retrieved on the next postback.
Render This is not an event; instead, at this stage of processing, the Page object calls this method on each control. All ASP.NET Web server controls have a Render method that writes out the control's markup to send to the browser.
Unload Raised for each control and then for the page.

Bellow is list that is more elaborate:
PreInit :The master page and content page are merged during the initialization stage of page processing so master page must be assigned before it that is why one can change master page dynamically only at PreInit event.
     This is the only event where programmatic access to master pages and themes is allowed. This event is not recursive, meaning that it is accessible only for the page itself and not for any of its child controls.
Init: The “Init” event is fired reclusively for the page itself and for all the child controls in the hierarchy. The “Init” event is fired first for the most bottom control in the hierarchy, and then fired up the hierarchy until it is fired for the page itself. The initialization event can be overridden using the OnInit method. At this event the Viewstate tracking is not yet enabled.
                You can call the “Page. TrackViewState ()” method at the start of the “Init” event and thus enabling tracking. In this case, the “Init” event will behave exactly like the “InitComplete” event, to be discussed next.

InitComplete: signals the end of the initialization phase. It is at the start of this event that tracking of the ASP.NET Viewstate is turned on by using TrackViewState() method of control      e.g. Page. TrackViewState ()
LoadViewState: Occurs only at postback. This is a recursive event first it fires for control at bottom of hierarchy then for page itself.Here, the Viewstate which has been saved in the __VIEWSTATE during the previous page visit (via the SaveViewState event) is loaded and then populated into the control hierarchy.

 

LoadPostbackdata: Occurs only at postback. This is a recursive event first it fires for control at bottom of hierarchy then for page itself. During this event, the posted form data is loaded into the appropriate controls. This is why when one post a form, he find that the posted data is loaded again into the appropriate controls.

   Viewstate is not responsible for preserving posted data.Try to disable the Viewstate on a TextBox control or even on the entire page, and you will find that the posted data is still preserved. It is due to the virtue of the LoadPostbackdata event.
PreLoad: The PreLoad event is raised after all postback data processing and before the Load event. There is a second attempt to load postback data before the OnLoadComplete event.
Load: This is a recursive event first it fires for control at bottom of hierarchy then for page itself. All objects are first arranged in the page DOM (called the Control Tree in ASP.NET) .Here, You can access view state information and Web form POST data from this event. You can also access other server controls within the page's control hierarchy.
The Load event can be overridden by calling OnLoad

Control events (RaisePostbackEvent): Occurs only at postback. What this event does is inspect all child controls of the page and determine if they need to fire any postback events. If it finds such controls, then these controls fire their events.

   If the page contains validator controls, check the IsValid property of the Page and of individual validation controls before performing any processing.


LoadComplete: Use this event for tasks that require that all other controls on the page be loaded.
Prerender: This is a recursive event first it fires for page at top of hierarchy then for control down the hierarchy. Use the event to make final changes to the contents of the page or its controls before the rendering stage begins. Any changes in the view state of the server control can be saved during this event. Such changes made in the rendering phase will not be saved.
PreRenderComplete: This is the last event raised before the page's view state is saved. The right place to put in the last logic before ViewState is saved.

SaveViewstate: This is a recursive event like Init. During this event, the Viewstate of the page is constructed and serialized into the __VIEWSTATE hidden field.


SaveStateControl: In Asp.net 2.0 the Control State is used to store the UI state of a control such as the sorting and paging of a DataGrid. The Control State is also serialized and stored in the same __VIEWSTATE hidden field. The Control State cannot be set off.
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, but the changes will not be retrieved on the next postback.

Render: This is a recursive called method on page and it’s control like Init. The Render method takes an HtmlTextWriter object as a parameter and uses that to output HTML to be streamed to the browser.

Changes can still be made at this point, but they are reflected to the client only. Can be used to move Viewstate hidden field downward for SEO purpose.

Unload: This is a recursive event first it fires for control at bottom of hierarchy then for page itself. Use this event to do final cleanup work, such as closing open files and database connections, or finishing up logging or other request-specific tasks.

   You should destroy any objects or references you have created in building the page. At this point, all processing has occurred and it is safe to dispose of any remaining objects, including the Page object.


No comments:

Post a Comment