In this article I’ll
talk about the ASP.NET page life cycle. We will try to see what all events are important
for an ASP.NET developer and what can be achieved in these events.
As an ASP.NET
developer, it is essential to understand the ASP.NET application life cycle and
Page life cycle. With the ease of development provided by Visual Studio,
sometimes new programmers get started with writing ASP.NET pages without
understanding the Application and Page life cycle.
From an end user's
perspective, a request for a web page is made to the web server and web server
will return the page to the user. So simple isn’t it J
However for a little
bit technical users, we can also state that the web server will receive the
request, perform some server side activities like reading from database,
process data received and return the output back to the user.
As an ASP.NET
developer, one need to understand how this request is being processed i.e. the Application life cycle and how the web
page is being processed and getting served to the user i.e. the 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.
Steps like;
initialization, instantiating controls, restoring and maintaining state,
running event handler code, and rendering.
It is important for
you to understand the page life cycle so that you can write code better and at
the appropriate stage of life-cycle for the effect you intend.
Let’s see Page life cycle stages
Stage
|
Description
|
Page request
|
The
page request occurs before the page life cycle begins. When the page is
requested by a user, ASP.NET determines whether the page needs to be parsed
and compiled (therefore beginning the life of a page), or whether a cached
version of the page can be sent in response without running the page.
|
Start
|
In
the start stage, page properties such
as Request and Response are set. At this stage, the page
also determines whether the request is a postback or a new request and sets
the IsPostBack property. The page also sets
the UICulture property.
|
Initialization
|
During
page initialization, controls on the page are available and each
control's UniqueID property is set. A master page and themes are
also applied to the page if applicable. If the current request is a postback,
the postback data has not yet been loaded and control property values have
not been restored to the values from view state.
|
Load
|
During
load, if the current request is a postback, control properties are loaded
with information recovered from view state and control state.
|
Postback event handling
|
If
the request is a postback, control event handlers are called. After that,
the Validate method of all validator controls is called, which sets
the IsValid property of individual validator controls and of the
page. (There is an exception to this sequence: the handler for the event that
caused validation is called after validation.)
|
Rendering
|
Before
rendering, view state is saved for the page and all controls. During the
rendering stage, the page calls the Render method for each control,
providing a text writer that writes its output to
the OutputStream object of the page's Response property.
|
Unload
|
The Unload event
is raised after the page has been fully rendered, sent to the client, and is
ready to be discarded. At this point, page properties such
as Response and Request are unloaded and cleanup is
performed.
|
As I was said
earlier; it is important for an ASP.NET developer and what can be achieved in
these events, similarly we also need to understand in which event or stage of
life cycle what is available to us.
For example ViewState is available in which all
events or stages of a life cycle – see below image.
We can find list of
all events available in Page Lifecycle at Microsoft
MSDN Library
When we use server
controls on ASP.net Page, we do not care about the individual controls life
cycle – Yes each individual server control has its own life cycle which is
similar to the page lifecycle.
For example: each
control’s Init and Load event occurs during the corresponding page events
Below image can show
you the flow of page life cycle – for detail information click
here (reference of Microsoft MSDN Library)
You can refer below
links for more information on Lifecycle
Hope next time when
you will be writing your code you will consider page lifecycle J