You are on page 1of 8

ASP.

NET Page Life Cycle Overview



In this post, I am going to explain Page Life Cycle Events in brief.

General Page Life Cycle Stages:
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.

Page request -> Start -> Initialization -> Load -> Postback event handling -> Rendering -> Unload

Page Life Cycle Events:

PreInit()
o Check for the IsPostBack property to determine whether this is the first time the
page is being processed.
o Create or recreate dynamic controls.
o Set master page dynamically.
o Set the Theme property dynamically.
o Read or set profile property values.

If Request is postback:
o The values of the controls have not yet been restored from view state.
o If you set control property at this stage, its value might be overwritten in the next
event.
Init()
o In the Init event of the individual controls occurs first, later the Init event of the
Page takes place.
o This event is used to initialize control properties.

InitComplete()
o Tracking of the ViewState is turned on in this event.
o Any changes made to the ViewState in this event are persisted even after the next
postback.

PreLoad()
o This event processes the postback data that is included with the request.

Load()
o In this event the Page object calls the OnLoad method on the Page object itself,
later the OnLoad method of the controls is called.
o Thus Load event of the individual controls occurs after the Load event of the page.

ControlEvents()
o This event is used to handle specific control events such as a Button controls Click
event or a TextBox controls TextChanged event.
In case of postback
o If the page contains validator controls, the Page.IsValid property and the validation
of the controls takes place before the firing of individual control events.

LoadComplete()
o This event occurs after the event handling stage.
o This event is used for tasks such as loading all other controls on the page.

PreRender()
o In this event the PreRender event of the page is called first and later for the child
control.
Usage:
o This method is used to make final changes to the controls on the page like
assigning the DataSourceId and calling the DataBind method.

PreRenderComplete()
o This event is raised after each control's PreRender property is completed.

SaveStateComplete()
o This is raised after the control state and view state have been saved for the page
and for all controls.

RenderComplete()
o The page object calls this method on each control which is present on the page.
o This method writes the controls markup to send it to the browser.

Unload()
o This event is raised for each control and then for the Page object.

Complete List of Page Methods & Events



Example:

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Page Life Cycle Events</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnEnter" runat="server" Text="Page Button"
onclick="btnEnter_Click" />
</div>
</form>
</body>
</html>

Default.aspx.CS

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
int index = 0;

#region PAGE LIFE CYCLE

protected void Page_PreInit(object sender, EventArgs e)
{
Response.Write("" + (++index).ToString() + " Pre-Init <br />");

}
protected override void OnInit(EventArgs e)
{
Response.Write("" + (++index).ToString() + " Init <br />");

}
protected void Page_InitComplete(object sender, EventArgs e)
{
Response.Write("" + (++index).ToString() + " Init Completed <br />");

}
protected void Page_Load(object sender, EventArgs e)
{
ViewState["test"] = "Test";
Response.Write("" + (++index).ToString() + " Load <br />");

}
protected override void OnPreRender(EventArgs e)
{
Response.Write("" + (++index).ToString() + " Pre Render <br />");

}
//protected override void Render(HtmlTextWriter writer)
//{
// //Response.Write("" + (++i).ToString() + " Render");

//}
protected override void OnUnload(EventArgs e)
{
// Response.Write("" + (++i).ToString() + " Unload");

}
public override void Dispose()
{
// Response.Write("" + (++i).ToString() + " Dispose");

}
protected override object SaveViewState()
{
Response.Write("" + (++index).ToString() + " Save View State <br />");
return base.SaveViewState();
}
public void RaisePostBackEvent(string eventArgument)
{
Response.Write("" + (++index).ToString() + " Raise Post Back Event <br />");

}
public void RaisePostDataChangedEvent()
{
Response.Write("" + (++index).ToString() + " Raise Post Data Change Event <br />");

}
protected override void LoadViewState(object o)
{
Response.Write("" + (++index).ToString() + " Load View State <br />");
base.LoadViewState(o);

}
private void Page_LoadComplete(object sender, System.EventArgs e)
{
Response.Write("" + (++index).ToString() + " Load Completed <br />");
}
// On Button Click Event
protected void btnEnter_Click(object sender, EventArgs e)
{
Response.Write("" + (++index).ToString() + " Button Click <br />");
}
#endregion
}

Output :
o When a page request sent to the web server initially.


o On button's click event


To download the source code, click here

For more details visit http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx

You might also like