Thursday, August 7, 2008

ASP.NET Page Life Cycle

When an ASP.NET page is requested by the client-browser, the page goes through a life cycle in which it performs a series of processing steps. These include initialization, instantiating controls, restoring and maintaining state, running event handler code, and rendering. Moreover, for custom controls, understanding the page life cycle is very important in order to correctly initialize controls, populate control properties with view-state data, and run any control behavior code.

Page Life-cycle Stages
When a web page is requested it goes through a number of stages to get completely built. In addition to the page life-cycle stages, there are application stages that occur before and after a request to the page. Following are the stages under which a page goes before getting rendered to the client.

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 or whether a cached version of the page can be sent in response without running the page.

Start: In this step, the HTTP 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's UICulture property is also set.

Page initialization: During page initialization, controls on the page are available and each control's UniqueID property is set. Any themes are also applied to the page. For the page post backs the controls values are not set in this stage from the view state.

Load: During load stage, if the current request is a postback, control properties are loaded with information recovered from view state and control state.

Validation: The Validate method of all validator controls is called, which sets the IsValid property of individual validator controls and of the page.
Postback event handling: If the request is a postback, any relevant event handlers are called.

Rendering: Before rendering, view state is saved for the page and all controls. During the rendering phase, the page calls the Render method for each control, providing a text writer that writes its output to the OutputStream of the page's Response property.

Unload: Unload is called 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 any cleanup is performed.

Life-cycle Events
At each stage of the page life cycle, the page raises certain events that can be used for custom coding. For control events, the event handlers must be bound to the enets. The following lists the page life-cycle events:
1. PreInit
2. Init
3. InitComplete
4. PreLoad
5. Load
6. Control events
7. LoadComplete
8. PreRender
9. SaveStateComplete
10. Render
11. Unload

2 comments:

Anonymous said...

Good Info, Sandeep

Kuldeep Chowhan said...

Thanks for the information

Post a Comment