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
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.
- 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.
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.
- 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.
- Validation: After the page is loaded, validation controls gets
fired and displays error messages. You just have to check whether the
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:
- Immediate Response events: For eg. Button click, link click, etc. These events trigger a postback immediately.
- 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.
- 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:
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.