Click here to Skip to main content
13,146,365 members (43,893 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

50.2K views
8 bookmarked
Posted 9 Feb 2015

ASP.NET Page Life Cycle

, 9 Feb 2015
Rate this:
Please Sign up or sign in to vote.
Page Framework Initialization - Page.Init

Processing of ASP.NET web page takes place in different stages on server side. Different events raised on different stages. This allows the web page to be in processing flow. Following are the web page processing stages and their respective events.

Remember that these stages occur independently for every page request.

Page Framework Initialization - Page.Init

In this stage, the ASP.NET creates the web page and generates all controls which are defined on the web page in the form of tags. If the page is not being requested for the first time, ASP.NET deserializes the view state and applies it to all controls.

User Code Initialization - Page.Load

If the page is being requested for the first time or it is a part of postback, the event Page.Load is always called. The required initialization of controls can be done in this stage. ASP.NET allows you to distinguish between the first time page is being loaded and it is being postback. This is just to boost the performance by loading the dynamic data on the very first load of page, not on the postback. On the postback, ASP.NET restores the control’s properties from the view state. To determine the current state of the page, you can use the IsPostBack property of the page like this:

If (!IsPostBack)
{
        //do something here.
}

Validation and Event Handling

Validation

Some validation controls are included in ASP.NET to validate other user input controls and error messages. These validation controls are self sufficient, which means you don’t need to take care of the validation events. The validation occurs after the Page.Load event and before any other event fired.

Event handling

At this stage, the web page is fully loaded and validated. Now ASP.NET fires all those events that have taken place since the last postback. Events have been categorized in the following two parts:

  1. Immediate response events: like button click, click on different links, image click, or image button click (that trigger the postback by calling the _doPostBack)
  2. Change Events: These events are fired in changing of selections or text change in text box. These events are called if the AutoPostBack property is set to TRUE for the control otherwise they fire the next time page is postback.

Automatic Data Binding - Page.PreRender

ASP.NET automatically performs updates and queries against your data source controls as a part of page life cycle. Essentially, there are two types of data source operations.

  1. Before firing of Page.PreRender event: Operations like insert, update or delete performed after all controls events but before Page.PreRender event fires.
  2. After firing of Page.PreRender event: The data source controls perform their queries and insert the retrieved data to their binding controls.

The Page.PreRender event is supposed to be the last action in the page life cycle before the page is rendered to HTML. During the pre-render, the page and controls are available so far, some actions can be performed here like storing some additional information to view state.

Cleanup - Page.Unload

At the end of the page life cycle, when web page has been rendered to HTML, the Page.Unload event fired. The .NET Framework garbage collector service runs to clean up the orphan objects to release the unnecessary occupied memory. When garbage collector is done with clean up, the Page.Dispose event fires. This is the end of the page life cycle.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Sohail Fareedi
Software Developer
Pakistan Pakistan
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionIts good and simple but.. Pin
Shivprasad koirala24-Jun-15 16:04
mvpShivprasad koirala24-Jun-15 16:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170915.1 | Last Updated 9 Feb 2015
Article Copyright 2015 by Sohail Fareedi
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid