Click here to Skip to main content
Click here to Skip to main content

[Explained]Life Cycle of an ASP.NET Page

, 3 Feb 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Life cycle of an ASP.NET page

Introduction

I believe this is the most difficult and basic topic in ASP.NET. The newbies find it very difficult to understand the life cycle of an ASP.NET page. So, I am doing my bit of tech service to explain the life cycle of an ASP.NET page.

First, you need to understand what is the Life Cycle of an ASP.NET page?

When you open a web page in the browser, it sends a request to the server and server processes this request and returns a page to the browser. This complete process is known as Life cycle of a page.

How it starts: The life of an ASP.NET page starts when the page is first requested by the user.
How it ends: It ends when the page is rendered completely to the browser.

Until now, it's simple. On the server side, the processing of an ASP.NET page is done in stages. At each stage, some events are fired. With these events, you can write your own code to handle any processing logic in ASP.NET page.

Now I will explain the different stages of ASP.NET page life cycle. I have seen many explanations of the Page Life cycle and none of them could make it into my favorite list. All the articles were very lengthy and complex. I will try to explain it in the simplest language with a clarity of thought.

Here are the list of stages:

I am writing this tip to explain the life cycle of the page. So I try to simulate and experience the complete life cycle of an ASP.NET page on my HP Notebook. What I will do? I will start my browser and open an ASP.NET page. Then will tell you the complete story of the ASP.NET page life cycle right from when I start typing the page URL in the browser to final rendering of the page in my browser.

Let's start!

  1. Browser makes a request:

    I open my browser Mozilla and type the URL of an ASP.NET website. Let's say I typed http://woodland.com/. What does it means? This means that I made a request to the browser to open this page for me. Browser will send my request to the server on which this page is hosted.

  2. Page framework initialization: Page.Init event fires

    ASP.NET checks whether the request is a new one or an old one. If the request is a new one, then ASP.NET creates the page. It generates the page with all the controls defined in the .aspx page.

    If the page request is an old one, then ASP.NET gets the data from View state and sets all the controls status View State information and page is returned to the browser.

  3. User Code Initialization: Page.Load event fires.

    In this event, initialization is done. Populating the dynamic controls or dropdown list is done in this event. This event always fires whether the page requested for the first time or page is requested as part of a postback. Initialization is to be done only on the first request. On a postback, you have to do nothing, ASP.NET restores the control properties automatically from View State information.

  4. Validation: After the page is loaded, validation controls gets fired and displays error messages. You just have to check whether the Page.IsValid is true or false.
  5. Event handling: Now the page is fully loaded and validated. This stage includes any events that fired after the last postback. There are 2 types of events in an ASP.NET page life cycle:

    1. Immediate Response events: For eg. Button click, link click, etc. These events trigger a postback immediately.

    2. Change Events: For example: Changing the selection in a dropdown list or in a Textbox. These events fire when the page is posted back next time.
  6. Browser receives response: Response and request properties of the page are unloaded and any cleanup operation if required is performed.

    Example: I believe that there is nothing better than an example to explain things. So I am providing you a page with a sample scenario.

I have created a page with a Textbox and a submit button. I have written some text in Textbox and click submit button triggering a Postback. Here is the list of events that fires in this example:

  1. Page.Init
  2. Page.Load
  3. Textbox.TextChanged
  4. Button.Click
  5. Page.PreRender
  6. Page.Unload

I hope the above explanation has given you a clear idea of how ASP.NET page executes and runs. You can use these events to write optimal and perfect code for your projects.

License

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

Share

About the Author

Subodh Raghav, a software engineer and certified web enthusiast, acquired his formal education and training at Aligarh Muslim University, India. He holds a Masters Degree in Computer Application and as of the present time; takes pride in working in the I.T. industry.
 
Subodh’s first article had been featured and published in EzineArticles.com and ArticlesBase.com among many others. As a programming enthusiast and a self-confessed “programming addict”, he has created various software applications to maximize the lifestyles of the modern internet users and other people who share his passion for computer technology. With over 60 original posts in his blog, he continues to impart his expertise to others by way of posting new and valuable information that can make a major difference in people’s lives.
 
Subodh’s hobbies and personal interests include writing informative blogs and surfing the internet; and he also takes inspiration from music and physical sports such as Cricket. Currently, he works for a IT Company in Noida, India; and is the author of his own blog.

Comments and Discussions

 
GeneralGood for beginner PinmemberPranay Rana3-Feb-14 8:33 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150123.1 | Last Updated 3 Feb 2014
Article Copyright 2014 by Subodh Raghav
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid