Introduction
Let's learn the ASP.Net page life cycle and how different events are fired during an ASP.NET page life cycle. 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. The following are the various stages or events of the ASP.Net page life cycle.

PreInit
- Check the IsPostBack property to determine whether this is the first time the page is being processed.
- Create or re-create dynamic controls.
- Set a master page dynamically.
- Set the Theme property dynamically.

Note. If the request is a postback, then the values of the controls have not yet been restored from the view state. If you set a control property at this stage, its value might be overwritten in the next event.
Init
- 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 to read or initialize control properties.
- The "Init" event is fired first for the bottom-most control in the hierarchy and then fired up the hierarchy until it is fired for the page itself.

InitComplete
- Until now, the ViewState values are not yet loaded. Hence you can use this event to make changes to the view state that you want to ensure are persisted after the next postback.
- Raised by the Page object.
- Use this event for processing tasks that require all initialization to be complete.

OnPreLoad
- 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 the view state for itself and all controls and then processes any postback data included with the Request instance.
- Loads ViewState: ViewState data are loaded to controls.
- Loads Postback data: Postback data are now handed to the page controls.

Load
- 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 page.
- This is the first place in the page lifecycle where all values are restored.
- Most code checks the value of IsPostBack to avoid unnecessarily resetting the state.
- You may also call Validate and check the value of IsValid in this method.
- You can also create dynamic controls in this method.
- Use the OnLoad event method to set properties in controls and establish database connections.

Control PostBack Event(s)
- ASP.NET now calls any events on the page or its controls that caused the PostBack to occur.
- Use these events to handle specific control events, such as a Button control's Click event or a TextBox control's TextChanged event.
- In a postback request, if the page contains validator controls, check the IsValid property of the Page and of individual validation controls before performing any processing.
- This is just an example of a control event. Here it is the button click event that caused the postback.

LoadComplete
- Raised at the end of the event-handling stage.
- Use this event for tasks that require that all other controls on the page be loaded.

OnPreRender
- Raised after the Page object has created all controls that are required in order to render the page, including child controls of composite controls.
- The Page object raises the PreRender event on the Page object and then recursively does the same for each child control. The PreRender event of individual controls occurs after the PreRender event of the page.
- The PreRender event of individual controls occurs after the PreRender event of the page.
- Allows final changes to the page or its control.
- This event takes place before saving ViewState, so any changes made here are saved.
- For example: After this event, you cannot change any property of a button or change any ViewState value.
- Each data-bound control whose DataSourceID property is set calls its DataBind method.
- Use the event to make final changes to the contents of the page or its controls.
nbsp;
OnSaveStateComplete
- 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 all controls.
- Any changes to the page or controls at this point will be ignored.
- Use this event to perform tasks that require the view state to be saved, but that do not make any changes to controls.

Render Method
- This is a method of the page object and its controls (and not an event).
- The Render method generates the client-side HTML, Dynamic Hypertext Markup Language (DHTML), and script that are necessary to properly display a control in the browser.
UnLoad
- This event is used for cleanup code.
- At this point, all processing has occurred, and it is safe to dispose of any remaining objects, including the Page object.
- Cleanup can be performed on:
- Instances of classes, in other words, objects
- Closing opened files
- Closing database connections.
- This event occurs for each control and then for the page.
- During the unload stage, the page and its controls have been rendered, so you cannot make further changes to the response stream.
- Suppose you attempt to call a method such as the Response. Write the method. Then the page will throw an exception.

EXAMPLES
Example 1. Control Values
In the following code, I have assigned the values to the label control on each event. When you run the code, you will see that in the "Page_UnLoad", the values are not assigned to the label. WHY? During the unload stage, the page and its controls have been rendered, so you cannot change the values.
Please observe the code comments and output. It will help you to clearly understand the concepts.
public partial class PageLifeCycle : System.Web.UI.Page
{
protected void Page_PreInit(object sender, EventArgs e)
{
// Work and assign values to label.
lblName.Text += "<br/>" + "PreInit";
}
protected void Page_Init(object sender, EventArgs e)
{
// Work and assign values to label.
lblName.Text += "<br/>" + "Init";
}
protected void Page_InitComplete(object sender, EventArgs e)
{
// Work and assign values to label.
lblName.Text += "<br/>" + "InitComplete";
}
protected override void OnPreLoad(EventArgs e)
{
// Work and assign values to label.
// If the page is post back, then label control values will be loaded from view state.
// E.g.: If you string str = lblName.Text, then str will contain viewstate values.
lblName.Text += "<br/>" + "PreLoad";
}
protected void Page_Load(object sender, EventArgs e)
{
// Work and assign values to label.
lblName.Text += "<br/>" + "Load";
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
// Work and assign values to label.
lblName.Text += "<br/>" + "btnSubmit_Click";
}
protected void Page_LoadComplete(object sender, EventArgs e)
{
// Work and assign values to label.
lblName.Text += "<br/>" + "LoadComplete";
}
protected override void OnPreRender(EventArgs e)
{
// Work and assign values to label.
lblName.Text += "<br/>" + "PreRender";
}
protected override void OnSaveStateComplete(EventArgs e)
{
// Work and assign values to label.
// But "SaveStateComplete" values will not be available during post back, i.e., View state.
lblName.Text += "<br/>" + "SaveStateComplete";
}
protected void Page_UnLoad(object sender, EventArgs e)
{
// Work and it will not affect label control, view state, and post back data.
lblName.Text += "<br/>" + "UnLoad";
}
}
Output
The first time the Page Load is output.

When you click on the Submit Button output.

The first time the page loads with EnableViewState="false".

When you click on the Submit Button output with EnableViewState="false":

Example 2. ViewState Values
Please observe the code comments and output. It will help you to clearly understand the concepts.
public partial class PageLifeCycle : System.Web.UI.Page
{
protected void Page_PreInit(object sender, EventArgs e)
{
// Work and assign values to label.
// Note: If page is post back or first time call and you have not set any values to ViewState["value"], then
// Convert.ToString(ViewState["value"]) is always empty.
ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "PreInit";
lblName.Text = Convert.ToString(ViewState["value"]);
}
protected void Page_Init(object sender, EventArgs e)
{
// Work and assign values to label.
// Note: If page is post back or first time call and you have not set any values to ViewState["value"] in previous events, then
// Convert.ToString(ViewState["value"]) is always empty.
ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "Init";
lblName.Text = Convert.ToString(ViewState["value"]);
}
protected void Page_InitComplete(object sender, EventArgs e)
{
// Work and assign values to label.
// Note: If page is post back or first time call and you have not set any values to ViewState["value"] in previous events, then
// Convert.ToString(ViewState["value"]) is always empty.
ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "InitComplete";
lblName.Text = Convert.ToString(ViewState["value"]);
}
protected override void OnPreLoad(EventArgs e)
{
// Work and assign values to label.
// Note: If the page is post back and you have set or not set any values to ViewState["value"] in previous events, then
// Convert.ToString(ViewState["value"]) will always have post back data.
// E.g.: If you string str = Convert.ToString(ViewState["value"]), then str will contain post back values.
ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "PreLoad";
lblName.Text = Convert.ToString(ViewState["value"]);
}
protected void Page_Load(object sender, EventArgs e)
{
// Work and assign values to label.
ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "Load";
lblName.Text = Convert.ToString(ViewState["value"]);
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
// Work and assign values to label.
ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "btnSubmit_Click";
lblName.Text = Convert.ToString(ViewState["value"]);
}
protected void Page_LoadComplete(object sender, EventArgs e)
{
// Work and assign values to label.
ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "LoadComplete";
lblName.Text = Convert.ToString(ViewState["value"]);
}
protected override void OnPreRender(EventArgs e)
{
// Work and assign values to label.
ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "PreRender";
lblName.Text = Convert.ToString(ViewState["value"]);
}
protected override void OnSaveStateComplete(EventArgs e)
{
// Work and assign values to label.
// But "SaveStateComplete" values will not be available during post back, i.e., View state.
ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "SaveStateComplete";
lblName.Text = Convert.ToString(ViewState["value"]);
}
protected void Page_UnLoad(object sender, EventArgs e)
{
// Work and it will not affect label control values, view state, and post back data.
ViewState["value"] = Convert.ToString(ViewState["value"]) + "<br/>" + "UnLoad";
lblName.Text = Convert.ToString(ViewState["value"]);
}
}
Output
During the first, the Time Page Load is output.

When you click on the Submit Button output:

During the first time, the page loads with EnableViewState="false":

When you click on the Submit Button, the output with EnableViewState="false":

Example 3. ViewState Values
Please observe the code comments and output. It will help you to clearly understand the concepts.
public partial class PageLifeCycle : System.Web.UI.Page
{
protected void Page_PreInit(object sender, EventArgs e)
{
Response.Write("<br/>" + "PreInit");
}
protected void Page_Init(object sender, EventArgs e)
{
Response.Write("<br/>" + "Init");
}
protected void Page_InitComplete(object sender, EventArgs e)
{
Response.Write("<br/>" + "InitComplete");
}
protected override void OnPreLoad(EventArgs e)
{
Response.Write("<br/>" + "PreLoad");
}
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("<br/>" + "Load");
}
protected void Page_LoadComplete(object sender, EventArgs e)
{
Response.Write("<br/>" + "LoadComplete");
}
protected override void OnPreRender(EventArgs e)
{
Response.Write("<br/>" + "PreRender");
}
protected override void OnSaveStateComplete(EventArgs e)
{
Response.Write("<br/>" + "SaveStateComplete");
}
protected void Page_UnLoad(object sender, EventArgs e)
{
// Runtime Error: Response is not available in this context.
// Response.Write("<br/>" + "UnLoad"); // Error
}
}
Output

Note. If you write Response.Write("<br/>" + "UnLoad"); in the Page_UnLoad event, then it will generate the Runtime Error "Response is not available in this context".


Allen MarshallPosted Apr 13, 2022, 2:29 PM
Thank you. Clear and effective discussion. FYI - A couple small typos - LiftCycle should be LifeCycle. label contrl values is supposed to be label control values. Work and It maybe should be Execute and it. view state and viewstate should be ViewState.
Pradeep ShetPosted Mar 9, 2022, 1:21 PM
Nicely written
Tom HoldenPosted Apr 19, 2021, 2:33 PM
I've been working with this product periodically since it was "ASP." Even so, some of the events in the page life cycle had remained foggy. Now they are much clearer. Thanks for taking the time to write this article.
Smeet ShahPosted Dec 2, 2020, 8:02 AM
Can anyone explain how the output is coming for the case "When you click on the Submit Button"
Devesh KumarPosted May 12, 2020, 11:12 AM
A very clear illustration what I was looking for. First time I am clear where to put what as my project has nested master pages and every page including the webform have interrelated different tasks. Thanks a lot.
Sourav Kumar DasPosted Mar 5, 2020, 10:58 PM
Nice and Informative Article.
waqar naeemPosted Feb 23, 2020, 6:00 AM
Great article ... like as you defined the different stages and condition then how would be t he change in page events.
Dinesh GabhanePosted Nov 7, 2019, 8:31 AM
Very Nice Article
Steve NaidamastPosted Jul 3, 2019, 10:21 AM
A wonderful a very clear explanation of the ASP.NET WebForm page life-cycle. However, by providing such an explanation, the author makes the WebForm page cycle appear more complicated than it actually is when working with it. The ASP.NET WebForm design was the zenith in web development technologies given the current topologies of the Internet. It was torn down simply to introduce something new and far, far, more complex than necessary...
Mallela VloggerPosted Nov 2, 2018, 12:27 PM
Cleanest explanation!!
Hitanshi MehtaPosted Oct 7, 2018, 6:25 AM
Very nice!!!!
Code AlonePosted Sep 14, 2018, 12:43 PM
Very nice explanation of Page Life Cycle
Chandan KumarPosted Apr 7, 2018, 2:04 AM
Very Help Full article
YashiPosted Dec 10, 2017, 8:45 AM
Hi can you share code
Patel SoniyaPosted Sep 28, 2017, 2:19 AM
Awesome Thanks...............
biswa barikPosted May 27, 2017, 3:45 PM
Very helpful article. Thanks..
Rajeev SinghPosted Dec 28, 2016, 12:48 PM
What is d/b page life cycle and event life cycle?
Rajeev SinghPosted Dec 28, 2016, 12:46 PM
Https://msdn.microsoft.com/en-us/library/ms178472.aspx
Amatya AgyeyPosted Jun 13, 2016, 1:16 AM
Good Effort .. Keep it up :)
Kamal ManiPosted Sep 24, 2015, 6:32 AM
Thanks
Neeraj PokhriyalPosted Aug 11, 2015, 3:28 PM
great
Dharmveer SinghPosted Jul 31, 2015, 4:05 AM
Thanks sir.., this help us to understand clearly...
Laxman ParmarPosted Jul 15, 2015, 8:28 AM
Awesome Thanks ..!!
Abbas JaberPosted Jul 15, 2015, 3:19 AM
Great Man :)
Upendra Pratap ShahiPosted Jul 3, 2015, 8:36 AM
good Mahesh Alle
Ahmed HashemPosted Jun 7, 2015, 5:04 PM
this helped me a lot thanks man
Anil SagarPosted Jun 7, 2015, 2:04 AM
Thanks Mahesh Sir ...it vary good articals
Anupam JainPosted Mar 18, 2015, 2:09 AM
I don have words to describe the excellence of dis article,,,,,,,,,,,,,,,,,,,,simply superb,,,,,,,,,,,,,,,,
Eric DouglasPosted Jan 6, 2015, 6:43 PM
Mahesh, great article and example. . . appreciate the time and effort! Going to share with our team. Best Regards.
BHAUMIK patelPosted Jan 1, 2015, 10:12 AM
Very good article... Thanks Mahesh for sharing
Teddy KurianPosted Dec 16, 2014, 12:06 AM
Good .Thanks Mahesh
Rasmita DashPosted Dec 3, 2014, 11:42 PM
superb.... It's very helpful
pabitra beheraPosted Dec 3, 2014, 12:22 AM
Its Good
Janak BeniwalPosted Jul 24, 2014, 3:35 AM
To the point and brief, it's Good...... Thanks for sharing.
Pramod LawatePosted May 28, 2014, 3:16 AM
Very awesome Mahesh......a way of represent your skill
Hussain AhmedPosted Mar 12, 2014, 12:39 PM
Good
Ramchand RepallePosted Oct 4, 2013, 1:30 PM
excellent article..
Mahavir vPosted Sep 12, 2013, 1:02 AM
Good article .......Thanks for sharing
Ravikumar GPosted Jun 12, 2013, 11:41 PM
Too Good ..