Lifecycle

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

ASP.

NET Page Life Cycle


A page in an ASP.NET application consists of several server controls. These are the
fundamental building blocks of an ASP.NET application. The Life cycle of an ASP.NET
page, depends on whether the page is requested for the first time or it is a postback.
Postback is a process by which a page can request for itself.

When the Page is requested for the first time


The Life Cycle of a page when requested for the first time:
Initializing: During this phase, the server creates an instance of the server control
Loading: During this phase, the instance of the control is loaded onto the page object in
which it is defined.
PreRendering: During this phase, the control is updated with the changes made to it.
This prepares the control for rendering.
Saving: During this phase, the state information of the control is saved. For example, if a
value is set for the control during the Load event, it is embedded in the HTML tag that
will be returned to the browser.
Rendering: During this phase, the server creates the corresponding HTML tag for the
control.
Disposing: During this phase, all cleanup tasks, such as closing files and database
connections opened by the control are performed.
Unloading: During this phase, all cleanup tasks, such as destroying the instances of
server control are performed. This is the final event in the life cycle of a server control

Life cycle when the page processed during a postback


event
The processing sequence in which a page is processed during a postback event is:
Initializing: During this phase, the server creates an instance of the server control
Loading view state: During this phase, the view state of the control posted by the client is
reloaded into the new instance of the control.
Loading: During this phase, the instance of the control is loaded onto the page object in
which it is defined.

Loading the postback data: During this phase, the server searches any data
corresponding to the control that is loaded in the data posted by the client.
PreRendering: During this phase, the control is updated with the changes made to it.
This prepares the control for rendering.
Saving state: During this phase, the change in the state of control between the current
request and the previous request of the page is saved. For each change, the corresponding
event is raised. For example, if the text of a textbox is changed, the new text is saved and
a text_change event is raised.
Rendering: During this phase, the server creates the corresponding HTML tag for the
control.
Disposing: During this phase, all cleanup tasks, such as closing files and database
connections opened by the control are performed.
Unloading: During this phase, all cleanup tasks, such as destroying the instances of
server control are performed. This is the final event in the life cycle of a server control
The events associated with the relevant page cycle phases are:

Page Initialization: Page_Init


View State Loading:LoadViewState

Postback data processing: LoadPostData

Page Loading: Page_Load

PostBack Change Notification: RaisePostDataChangedEvent

PostBack Event Handling: RaisePostBackEvent

Page Pre Rendering Phase: Page_PreRender

View State Saving: SaveViewState

Page Rendering: Page_Render

Page Unloading: Page_UnLoad

The following figure illustrates how the server controls on an ASP.NET page is processed
by the server:

Each request for an .aspx page that hits IIS is handed over to HTTP Pipeline. HTTP Pipeline is a chain of
managed objects that sequentially process the request and convert it to plain HTML text content. The start
point of HTTP Pipeline is the HttpRuntime class. The ASP.NET infrastructure creates each instance of this
class per AppDomain hosted within the worker process. HttpRuntime class picks up an HttpApplication
object from an internal pool and sets it to work on the request. It finds out what class has to handle the
request. The association between the resources and handlers are stored in the configurable file of the
application. In web.config and also in machine.config you will find these lines in <httpHandlers>
section.
If you run through the following program, it will be much easier to follow
<add verb="*" path="*.aspx" type="System.Web.UI.PageHandlerFactory"/>
This extension can be associated with HandlerClass or HandlerFactory class.
HttpApplication object gets the page object that implements the IHttpHandler
Interface. The process of generating the output to the browser is started when the
object calls ProcessRequest method.
Page Life Cycle
Once the HTTP page handler class is fully identified, the ASP.NET runtime calls the
handler's ProcessRequest to start the process. This implementation begins by
calling the method FrameworkInitialize(), which builds the control trees for the
page. This is a protected and virtual member of TemplateControl class, class from
which page itself derives.
Next the processRequest() makes page transits various phases: initialization,
loading of viewstate and postback data, loading of page's user code and execution
postback server-side events. Then page enters in render mode, the viewstate is
updated and HTML generated is sent to the output console. Finally page is unloaded
and request is considered completely served.
Stages and corresponding events in the life cycle of the ASP.NET page cycle:
Stage

Events/Method

Page Initialization

Page_Init

View State Loading

LoadViewState

Postback data processing

LoadPostData

Page Loading

Page_Load

PostBack Change Notification

RaisePostDataChangedEvent

PostBack Event Handling

RaisePostBackEvent

Page Pre Rendering Phase

Page_PreRender

View State Saving

SaveViewState

Page Rendering

Page_Render

Page Unloading

Page_UnLoad

Some of the events listed above are not visible at the page level. It will be visible if
you happen to write server controls and write a class that is derived from page.

Page Execution Stages


The first stage in the page life cycle is initialization. This is fired after the page's
control tree has been successfully created. All the controls that are statically declared
in the .aspx file will be initialized with the default values. Controls can use this event
to initialize some of the settings that can be used throughout the lifetime of the
incoming web request. Viewstate information will not be available at this stage.
After initialization, page framework loads the view state for the page. Viewstate is a
collection of name/value pairs, where control's and page itself store information that
is persistent among web requests. It contains the state of the controls the last time
the page was processed on the server. By overriding LoadViewState() method,
component developer can understand how viewstate is restored.
Once viewstate is restored, control will be updated with the client side changes. It
loads the posted data values. The PostBackData event gives control a chance to
update their state that reflects the state of the HTML element on the client.
At the end of the posted data changes event, controls will be reflected with changes
done on the client. At this point, load event is fired.
Key event in the life cycle is when the server-side code associated with an event
triggered on the client. When the user clicks on the button, the page posts back.
Page framework calls the RaisePostBackEvent. This event looks up for the event
handler and run the associated delegate.
After PostBack event, page prepares for rendering. PreRender event is called. This
is the place where user can do the update operations before the viewstate is stored
and output is rendered. Next stage is saving view state, all the values of the controls
will be saved to their own viewstate collection. The resultant viewstate is serialized,
hashed, base24 encoded and associated with the _viewstate hidden field.
Next the render method is called. This method takes the HtmlWriter object and
uses it to accumulate all HTML text to be generated for the control. For each control
the page calls the render method and caches the HTML output. The rendering
mechanism for the control can be altered by overriding this render method.
The final stage of the life cycle is unload event. This is called just before the page
object is dismissed. In this event, you can release critical resources you have such as
database connections, files, graphical objects etc. After this event browser receives
the HTTP response packet and displays the page.

You might also like