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.

StageDescription
Page requestThis stage occurs before the lifecycle begins. When a page is requested by the user, ASP.NET parses and compiles that page.
StartIn this stage, page properties such as Request and response are set. It also determines the Request type.
InitializationIn this stage, each control's UniqueID property is set. Master page is applied to the page.
LoadDuring this phase, if page request is postback, control properties are loaded with information.
Postback event handlingIn this stage, event handler is called if page request is postback. After that, the Validate method of all validator controls is called.
RenderingBefore 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.
UnloadAt 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 EventTypical Use
PreInitThis event is raised after the start stage is complete and before the initialization stage.
InitThis event occurs after all controls have been initialized.
We can use this event to read or initialize control properties.
InitCompleteThis 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.
PreLoadThis event is occurs before the post back data is loaded in the controls.
LoadThis event is raised for the page first time and then recursively for all child controls.
Control eventsThis event is used to handle specific control events such as Button control' Click event.
LoadCompleteThis 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.
PreRenderThis event occurs after the page object has created all controls that are required in order to render the page.
PreRenderCompleteThis event occurs after each data bound control whose DataSourceID property is set calls its DataBind method.
SaveStateCompleteIt is raised after view state and control state have been saved for the page and for all controls.
RenderThis is not an event; instead, at this stage of processing, the Page object calls this method on each control.
UnloadThis event raised for each control and then for the page.