When an ASP.NET page runs, 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. It is important for you to understand the page life cycle
- General Page Life-cycle Stages
- Life-cycle Events
- Additional Page Life Cycle Considerations
- Catch-Up Events for Added Controls
- Data Binding Events for Data-Bound Controls
- Login Control Events
In general terms, the page goes through the stages outlined in the following table. In addition to the page life-cycle stages, there are application stages that occur before and after a request but are not specific to a page.
Some parts of the life cycle occur only when a page is processed as a postback. For postbacks, the page life cycle is the same during a partial-page postback (as when you use an UpdatePanel control) as it is during a full-page postback.
1) Page request
The page request occurs before the page life cycle begins. When the page is requested by a user,
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.
During page initialization, controls on the page are available and each control’s UniqueIDproperty 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.
During load, if the current request is a postback, control properties are loaded with information recovered from view state and control state.
5) Postback event handling
If the request is a postback, control event handlers are called. After that, the Validatemethod of all validator controls is called, which sets the IsValid property of individual validator controls and of the page
Before rendering, view state is saved for the page and all controls. During the rendering stage, the page calls the Render(HtmlTextWriter) method for each control, providing a text writer that writes its output to the OutputStream object of the page’s Responseproperty.
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 CYLE EVENTS
The processing sequence in which a page is processed during a postback event is:
Initializing: During this phase, the server creates an instance of the server control
Loading view state: During this phase, the view state of the control posted by the client is reloaded into the new instance of the control.
Loading: During this phase, the instance of the control is loaded onto the page object in which it is defined.
Loading the postback data: During this phase, the server searches any data corresponding to the control that is loaded in the data posted by the client.
PreRendering: During this phase, the control is updated with the changes made to it. This prepares the control for rendering.
Saving state: During this phase, the change in the state of control between the current request and the previous request of the page is saved. For each change, the corresponding event is raised. For example, if the text of a textbox is changed, the new text is saved and a text_change event is raised.
Rendering: During this phase, the server creates the corresponding HTML tag for the control.
Disposing: During this phase, all cleanup tasks, such as closing files and database connections opened by the control are performed.
Unloading: During this phase, all cleanup tasks, such as destroying the instances of server control are performed. This is the final event in the life cycle of a server control
The events associated with the relevant page cycle phases are:
- Page Initialization: Page_Init
- View State Loading:LoadViewState
- Postback data processing: LoadPostData
- Page Loading: Page_Load
- PostBack Change Notification: RaisePostDataChangedEvent
- PostBack Event Handling: RaisePostBackEvent
- Page Pre Rendering Phase: Page_PreRender
- View State Saving: SaveViewState
- Page Rendering: Page_Render
- Page Unloading: Page_UnLoad