ASP.NET Page Life Cycle
ASP.NET Page Life Cycle determines series of processing steps which can be categorised as:-
1) Initialization of the page
2) Creation of instance of the controls
3) Restoration and Maintenance of the state
4) Event handler execution
5) Rendering of page.
It is important to understand page life cycle as it helps in writing code at appropriate stage of life cycle so that their can be desired effects that one intends.
Page Life Cycle Stages
1) Page request:A page request by user occurs before life cycle of the page begins. When user request the page, ASP.NET determines whether the page needs to be compiled(run for first time) or a cached page is returned in response of the request without running the page, this enforces beginning of page life cycle.
2)Start: In the start stage the page determines whether request is postback or new request and sets IsPostBack property accordingly. The page properties Request and Response are also set.
3)Initialization:In page initialization stage,UniqueId property of the controls which are available on the page is set thereby giving unique id to each control. In case of postback request, postback data is not yet loaded and property values of controls are not initialized to the values to be obtained from view state. This is done in next Load stage.
4) Load:In case of postback request, postback data and controls property values are set to the information obtained from control and view state.
5) Event handling of Postback request:Event handlers are called in case of postback request. For validator controls validate method is called in which IsValid property of the validator controls and of the page is set.
6)Rendering:The view state of the page is saved before rendering. Render method is called by the page for each control, a text writer is provided which writes it output to Outputstream object of the page's Response property.
7)Unload:As the name suggest this event is performed when page is fully rendered, is sent to the client and can be discarded. Response and Request page properties are unloaded thereby performing cleanup work.
Page Life Cycle Events
The page raises events within each stage of the lifecycle of a page which can be handled to run the code.The event handlers are binded to the event in case of control event.
Page Events
1) PreInit:This event is raised just after the start stage completes. It checks the IsPostBack property to determine whether request is postback or not i.e whether page is processed for the first time or not. It sets master page and theme property dynamically,create dynamic controls etc. This event can be handled by Overloading the OnPreInit metthod or creating a Page-PreInit handler.
2) Init:In this event control tree is built and is raised after all control's have been initialized. This event can be handled by Overloading the OnInit metthod or creating a Page-Init handler.
3) InitCompleteAfter all initialization of the page and its controls are completed this event is raised.This event can be used to make changes to view state as till now view state values are not loaded. This changed view state values will persisted after next postback.
4) PreLoad:Raised after loading viewstate data to controls and postback data are now handed to the page controls. This event can be handled by overloading the OnPreLoad method or creating a Page_PreLoad handler.
5) Load:For Postback request previous state has been restored. Load event typically checks for PostBack and then sets control properties. This method is used for most code, since this is the first place in the page lifecycle that all values are restored. Most code checks the value of IsPostBack to avoid unnecessarily resetting state.
6) Control:These events are used to handle specific control events like button control click event, dropdown's selectedindexchange event, textbox control TextChanged event etc.
7) LoadComplete:It signals end of Load i.e end of event handling stage.
8) PreRender:It allows final changes to the page or its control. This event takes place after all regular PostBack events have taken place. This event takes place before saving ViewState, so any changes made here are saved.
9) SaveStateComplete:Before this event the view state for the page and its controls is set. Any changes to the page's controls at this point or after are ignored.
10) Render:This is not an event but a method of the page object and its controls. ASP.NET calls this method on each of the page's controls to get output. The Render method generates the client-side HTML, Dynamic Hypertext Markup Language (DHTML), and script that are important to display a control at the browser properly.
11)UnLoad:Cleanup work such as closing control related database connections for controls,closing open files and database connections for page etc is done by this event.This event is first called for controls and then for page. Objects or references one creates in building the page are destroyed during this event. Since all processing is finished now it is safe to destroy any of remaining objects, including the page object.
0 Comment(s)