Understanding ASP.NET Page Lifecycle

Every time you request for a page in ASP.NET application, a series of stages are proceeded with. This article will help you in understanding those stages involved in the Page Lifecycle.

Request for a page arises at two different situations: when user requests page for the first time or when the page is called by itself (during form submit or change of control values).

The lifecycle is different in both the cases. That is depicted in the flowchart below:


Lifecycle stages have events associated with it. You can override these events in your code. Core functionality of each stage and its associated event is given below:

Page Initialization: Page and all the controls in the page are initialized. Page_Init event is triggered after all controls are initialized. You can use this event to read as well as to initialize the control properties. You can override page initialization using OnInit method.

Load View State: This stage is involved only when postback happens. Page data that was last submitted is stored in the View State. Now, the view state is validated and the persisted information is made available to the User, when the page is posted back. Event associated with this stage is LoadViewState. Sample code fragment overriding LoadViewState method is given below:
protected override void LoadViewState(Object tempObj)
txtUserName.Text = (string) ViewState[“UserName”];
txtBranchCode.Text=(string) ViewState[“BranchCode”];

Load Postback Data: This stage is also performed only when the page is posted back. If you have server controls in your page and if those server controls implement the interface IPostBackDataHandler, then its LoadPostData method is called and its last submitted value is posted back.

Load: Load stage is where the page gets loaded. If the page is posted back, then the above two stages would have been performed and the page will retain its previous state. If the page is loaded for the first time, then its initializations are done and it is ready for the user to access. Event associated with this phase is Page_Load.

You can override this event by writing corresponding code in OnLoad method. If you have any logic to be performed at the time of loading, then you can write it in this method. For example, if the user did not enter the branch code then by default it should be assigned with value br01. You can perform this logic with the following code:

protected override void OnLoad(EventArgs evt)
If(ViewState[“BranchCode”] == null)
string defaultBranch = “br01”;
ViewState[“BranchCode”] = defaultBranch;

Raise Postback Changes: This stage is also in place only when the page is posted back. The controls are now loaded with the previous submitted values. What if you change a textbox control value? The textbox change event will be triggered and the page is posted back. Now this changed value has to be updated and displayed correctly after post back.

How is it updated? When you change a control’s value, a Boolean flag is set internally to identify that the control has a change. After all controls are updated with the postback data, ASP.NET checks for the Boolean flag. If it is set for any control, then its RaisePostDataChanged event is fired. This event is a server side event. After this event got fired, the control which caused this postback will be handled by a client side event called RaisePostBackEvent.

Pre-Rendering: Final changes to the control are performed at this stage. The code is now ready for rendering. Its corresponding event is Page_PreRender which can be overridden by OnPreRender method.

Save View State: Values that you currently recorded in this page has to persist when the page is loaded again. This can be done by saving the field values in the view state. SaveViewState() method is called for all controls and the values are passed as hidden fields in the same page. It will then be rendered in HTML and made available to the User next time when the page is loaded.

Rendering: HTML tag for each control is constructed by calling the control’s Render method. This method gets HTMLTextWriter object as input parameter, uses this object to write the output to outputstream. All these HTML tags constructed are then assembled together and sent to the browser. You can override this method to write your own HTML statements instead of statements and tags that are created automatically. Page_Render is its corresponding event.

Disposal: Controls you use in your page might have used files or established database connectivity. These files and database related connections are closed in this stage of lifecycle. You can also clean the object references that are no longer going to be used. You can even dispose the Page object.

Unloading: When the page is displayed to the user and is no longer required, you can unload the page. The request and response properties of the page are unloaded. Page_Unload is the corresponding event.

FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.


Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage


Recommended Resource

| Understanding ASP.NET Page Lifecycle | Exploring Different Stages of Memory Management in .NET | How to Implement Forms Authentication Provider in .NET | How to Implement Toolbox Support in .NET? | How to Read and Write Files with Streams in ASP.NET? | How to Use a Custom Web Control in VS.NET? | Implementing .NET Passport Authentication in Web Applications Using Passport Authentication Provider | Using Atlas Architecture in ASP.NET |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2023 - All Rights Reserved.