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 developers to understand the page life cycle so that the developers can write code at the appropriate life-cycle stage for the intended effect.
Additionally, if developing custom controls, developers must be familiar with the page life cycle in order to correctly initialize controls, populate control properties with view-state data, and run any control behavior code.
The important phases in the page lifecycle
| Phase |
What happened |
Event raised/Method executed |
| Initialize |
Initialize settings needed during the lifetime of the incoming Web request. |
Init event (OnInit method) |
| Load view state |
At the end of this phase, the ViewState property of a control is automatically populated. A control can override the default implementation of the LoadViewState method to customize state restoration. |
LoadViewState method |
| Process postback data |
Process incoming form data and update properties accordingly.
Note Only controls that process postback data participate in this phase. |
LoadPostData method
(if IPostBackDataHandler is implemented) |
| Load |
Perform actions common to all requests, such as setting up a database query. At this point, server controls in the tree are created and initialized, the state is restored, and form controls reflect client-side data. |
Load event
(OnLoad method) |
| Validation |
During validation, the Validate method of all validator controls is called, which sets the IsValid property of individual validator controls and of the page. |
Validate method |
| Send postback change notifications |
Raise change events in response to state changes between the current and previous postbacks.
Note Only controls that raise postback change events participate in this phase. |
RaisePostDataChangedEvent method
(if IPostBackDataHandler is implemented) |
| Handle postback events |
Handle the client-side event that caused the postback and raise appropriate events on the server.
Note Only controls that process postback events participate in this phase. | RaisePostBackEvent method
(if IPostBackEventHandler is implemented) |
| Prerender |
Perform any updates before the output is rendered. Any changes made to the state of the control in the prerender phase can be saved, while changes made in the rendering phase are lost. |
PreRender event
(OnPreRender method) |
| Save state |
The ViewState property of a control is automatically persisted to a string object after this stage. This string object is sent to the client and back as a hidden variable. For improving efficiency, a control can override the SaveViewState method to modify the ViewState property.
|
SaveViewState method |
| Render |
Generate output to be rendered to the client. |
Render method |
| Dispose |
Perform any final cleanup before the control is torn down. References to expensive resources such as database connections must be released in this phase. |
Dispose method |
| Unload |
Perform any final cleanup before the control is torn down. Control authors generally perform cleanup in Dispose and do not handle this event. |
UnLoad event (On UnLoad method) |
Note: The CreateChildControls method is not listed in the table because it is called whenever the ASP.NET page framework needs to create the controls tree and this method call is not limited to a specific phase in a control's lifecycle.
For example, CreateChildControls can be invoked when loading a page, during data binding, or during rendering.
Data Binding Events for Data-Bound Controls
Data binding events occur when the Data-Bound control (
GridView,
DetailsView,
FormView, and etc.) bind to a data source.
The following table summarize data binding events for data-bound controls (such as
GridView,
DetailsView, and
FormView controls),
and the relationship between the page life cycle and data binding events.
| Data binding events |
Typical Use |
| DataBinding |
This event is raised by data-bound controls before the PreRender event of the containing control (or of the Page object) and marks the beginning of binding the control to the data.
If the data source controls are not used, developers can use this event to open database connections programmatically.
|
|
|
RowCreated (GridView only)
ItemCreated (DataList, DetailsView, SiteMapPath, DataGrid, FormView, and Repeater controls)
|
Use this event to manipulate content that is not dependent on data binding. For example, at run time, you might programmatically add formatting to a header or footer row in a GridView control.
|
| DataBound |
This event marks the end of data-binding operations in a data-bound control.
Use this event to format data bound content or to initiate data binding in other controls that depend on values from the current control's content.
|