83
ASP.NET Page Lifecycle
In ASP.NET, a web page has execution lifecycle that includes various phases. These phases include initialization, instantiation, restoring and maintaining state etc. it is required to understand the page lifecycle so that we can put custom code at any stage to perform our business logic.
Page Lifecycle stages
The following table contains the lifecycle stages of ASP.NET web page.
Stage | Description |
---|---|
Page request | This stage occurs before the lifecycle begins. When a page is requested by the user, ASP.NET parses and compiles that page. |
Start | In this stage, page properties such as Request and response are set. It also determines the Request type. |
Initialization | In this stage, each control’s UniqueID property is set. Master page is applied to the page. |
Load | During this phase, if page request is postback, control properties are loaded with information. |
Postback event handling | In this stage, event handler is called if page request is postback. After that, the Validate method of all validator controls is called. |
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 | At this stage the requested page has been fully rendered and is ready to terminate.at this stage all properties are unloaded and cleanup is performed. |
A requested page first loaded into the server memory after that processes and sent to the bowser. At last it is unloaded from the server memory. ASP.NET provides methods and events at each stage of the page lifecycle that we can use in our application. In the following table, we are tabled events.
ASP.NET Life Cycle Events
Page Event | Typical Use |
---|---|
PreInit | This event is raised after the start stage is complete and before the initialization stage. |
Init | This event occurs after all controls have been initialized. We can use this event to read or initialize control properties. |
InitComplete | This event occurs at the end of the page’s initialization stage. We can use this event to make changes to view state that we want to make sure are persisted after the next postback. |
PreLoad | This event is occurs before the post back data is loaded in the controls. |
Load | This event is raised for the page first time and then recursively for all child controls. |
Control events | This event is used to handle specific control events such as Button control’ Click event. |
LoadComplete | This event occurs at the end of the event-handling stage. We can use this event for tasks that require all other controls on the page be loaded. |
PreRender | This event occurs after the page object has created all controls that are required in order to render the page. |
PreRenderComplete | This event occurs after each data bound control whose DataSourceID property is set calls its DataBind method. |
SaveStateComplete | It is raised after view state and control state have been saved for the page and for all controls. |
Render | This is not an event; instead, at this stage of processing, the Page object calls this method on each control. |
Unload | This event raised for each control and then for the page. |
Next TopicASP.NET WF Introduction