When you run asp.net page goes through
life cycle in which performs series of steps.
General page life cycle goes with following
When ASP.NET gets a page request, it
decides whether to parse and compile the page, or there would be a cached
version of the page; accordingly the response is sent.
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.
At this stage, the controls on the
page are assigned unique ID by setting the UniqueID property and the themes are
applied. For a new request, postback data is loaded and the control properties
are restored to the view-state values.
At this stage if current request is post
back, control properties are set using the view state and control state values.
If the request is a postback, control
event handlers are called. After that, the Validate method of all validator
controls is called, which sets the IsValid property of individual validator
controls and of the page. (There is an exception to this sequence: the handler
for the event that caused validation is called after validation.)
At this stage, view state for the page
and all controls are saved. The page calls the Render method for each control
and the output of rendering is written to the OutputStream class of the
Response property of page.
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.
Page life cycle events:
For each stage of the life cycle of a
page, the page raises events that you can handle to run your own code.
Check the IsPostBack property to determine whether this is the first
time the page is being processed.
Create or re-create dynamic controls and gets and sets profile property
Set a master page dynamically and set the theme property dynamically.
This event can be handled by overloading the OnPreInit method or
creating a Page_PreInit handler.
protected void Page_PreInit(object sender, EventArgs e)
//page PreInit code here
This event fires after each control has been initialized.
Each control's UniqueID is set and any skin settings have been applied.
Use this event is used to read or initialize control properties and the
control tree is built
This event can be handled by overloading the OnInit method or creating a
protected void Page_Init(object sender, EventArgs e)
//page Init code here
This event is used to track view state, in this event view state values
are not loaded.
Use this event for processing tasks that require all initialization to
protected void Page_InitComplete(object sender, EventArgs e)
//page Init complete code here
LoadViewState event allows loading
view state information into the controls.
During this phase, the contents of all the
input fields are defined with the <form> tag are processed.
Raised after the page loads view state for itself and all controls, and
after it processes postback data that is included with the Request instance.
Before the Page instance raises this event, it loads view state for
itself and all controls, and then processes any postback data included with the
protected void Page_OnPreLoad(object sender, EventArgs e)
//page OnPreLoad code here
The Page object calls the OnLoad method on the Page object, and then
recursively does the same for each child control until the page and all
controls are loaded.
The Load event of individual controls occurs after the Load event of the
This is the first place in the page lifecycle that all values are
Most code checks the value of IsPostBack to avoid unnecessarily
You may also call Validate and check the value of IsValid in this
You can also create dynamic controls in this method.
Use the OnLoad event method to set properties in controls and establish
protected void Page_Load(object sender, EventArgs e)
//page load code here
1. In this event loading process is
completed, control event handlers are run, and page validation takes place.
2. Use this event for tasks that
require that all other controls on the page be loaded.
protected void Page_LoadComplete(object sender, EventArgs e)
//page LoadComplete code here
Raised after the Page object has created all controls that are required
in order to render the page, including child controls of composite controls
Use the event to make final changes to the contents of the page or its
controls before the rendering stage begins.
protected void Page_OnPreRender(object sender, EventArgs e)
//page OnPreRender code here
Raised after view state and control state have been saved for the page
and for all controls.
Before this event occurs, ViewState has been saved for the page and for
Any changes to the page or controls at this point will be ignored.
Use this event perform tasks that require the view state to be saved,
but that do not make any changes to controls.
protected void Page_OnSaveStateComplete(object sender, EventArgs e)
//page OnSaveStateComplete code here
This is not an event; this is a method of the page object and its
All ASP.NET Web server controls have a Render method that writes out the
control's markup to send to the browser.
The Render method generates the client-side HTML, Dynamic Hypertext
Markup Language (DHTML), and script that are necessary to properly display a
control at the browser.
1. Last event called in the asp.net page life
Final cleanup is done and all resources and references, such as database
connections, are freed.
This event can be handled by modifying the OnUnLoad method or creating a
protected void Page_UnLoad(object sender, EventArgs e)
//page UnLoad code here