Вы находитесь на странице: 1из 14

ASP.

Net Page Life Cycle

~ By~ Vaibhav Jape

Table of Contents

Introduction General Page Life-cycle Stages Life-cycle Events Stage and Event Mapping Catch-Up Events for Added Controls Data Binding Events for Data-Bound Controls Login Control Events

An SEI CMMI Level 3 Company

www.synechron.com

Introduction

When an ASP.NET page runs, the page goes through a life cycle in which it performs a series of processing steps. These include initialization, instantiating controls, restoring and maintaining state, running event handler code, and rendering. Important to understand the page life cycle so that you can write code at the appropriate life-cycle stage for the effect you want.

An SEI CMMI Level 3 Company

www.synechron.com

General Page Life-cycle Stages


Page request
Occurs before page life cycles. Asp.Net decides whether to parse and compile the page (which start page life cycle) or use cached version of the page. Sets Request and Response properties Depending upon whether the page is posted back (round tripped) IsPostback Property is set. Controls on the page are available and UniqueID property is set. Master pages and themes are applied If its post-back the post back data is not available and control states from view states are not available. If its post-back control properties are loaded with information recovered from view state and control state If its a post-back Control event handlers are called. Then Validate() method of all validator controls is called. ViewState is saved before rendering. During rendering Render method is called for each control. Raised after page is successfully rendered and sent to client. Response and Request properties are cleared. GC comes into action.

Start

Initialization

Load

Post-back event handling Rendering


Unload

An SEI CMMI Level 3 Company

www.synechron.com

Page Life Cycle Events


PreInit
Raised after start stage is complete and before initialization begins Used for
Check properties like IsPostback, IsCallback and IsCrossPagePostBack Create or re-create dynamic controls Set Master Pages dynamically Apply themes Read or set profile properties

Init
Raised after all controls have been initialized and skin settings have been applied Note: Init event of individual control occurs before the Init event of the page Used for read or initialize control properties Raised at the end of InitComplete Only one operation takes place in-between. i.e. Tracking of View State changes is turned on. What is view state? Used for making changes to ViewState. Raised after page load view state for itself and controls and after it processes postback data included with Request Instance.

InitComplete

PreLoad

An SEI CMMI Level 3 Company

www.synechron.com

Page Life Cycle Events Contd Load


Page object calls OnLoad method on itself and on all controls The load event of all control occurs AFTER load event of page. Used for
setting properties of controls and database connections etc

Control Events
Specific control events like Click of a button Raised at the end of event handling stage Used when all the controls on the page are loaded. Raised after all controls are created in Page object Page Object calls PreRender on itself and recursively on all controls Used for making final changes in page. Raised after each data bound control whose DataSourceID property is set calls its DataBind method. Raised after view state and control state have been saved for the page and for all controls.

LoadComplete PreRender

PreRenderComplete
SaveStateComplete

An SEI CMMI Level 3 Company

www.synechron.com

Page Life Cycle Events Contd


Render
It is a stage of processing and not an event. Page object calls on each control. Raised for each control and then on page. Use this event for clean-up activity

Unload

An SEI CMMI Level 3 Company

www.synechron.com

Stage and Event Mapping

An SEI CMMI Level 3 Company

www.synechron.com

Life Cycle Events Logical flow of request process(contd) Step 3 (P: ASP.NET page): Once the HttpHandler logic executes, the ASP.NET page object is created. While the ASP.NET page object is created, many events are fired which can help us to write our custom logic inside those page events. There are 6 important events which provides us placeholder to write logic inside ASP.NET pages Init, Load, validate, event, render and unload. Step4 (M: HttpModule): Once the page object is executed and unloaded from memory, HttpModule provides post page execution events which can be used to inject custom post-processing logic. There are 4 important post-processing events PostRequestHandlerExecute, ReleaserequestState, UpdateRequestCache and EndRequest.

An SEI CMMI Level 3 Company

www.synechron.com

Life Cycle Events

Section HttpModule

Event BeginRequest

Description This event signals a new request; it is guaranteed to be raised on each request. This event signals that ASP.NET runtime is ready to authenticate the user. Any authentication code can be injected here. This event signals that ASP.NET runtime is ready to authorize the user. Any authorization code can be injected here.

HttpModule

AuthenticateRequest

HttpModule

AuthorizeRequest

An SEI CMMI Level 3 Company

www.synechron.com

In ASP.NET, we normally use outputcache directive to do caching. In this event, ASP.NET runtime determines HttpModule ResolveRequestCache if the page can be served from the cache rather than loading the patch from scratch. Any caching specific activity can be injected here. This event signals that ASP.NET runtime is ready to acquire session variables. HttpModule AcquireRequestState Any processing you would like to do on session variables. This event is raised just prior to handling control to the HttpHandler. HttpModule PreRequestHandlerExecute Before you want the control to be handed over to the handler any preprocessing you would like to do. Httphandler logic is executed. In this HttpHandler ProcessRequest section, we will write logic which needs to be executed as per page extensions.
An SEI CMMI Level 3 Company

www.synechron.com

Page

Init

Page

Load

Page

Validate

Render

This event happens in the ASP.NET page and can be used for: Creating controls dynamically, in case you have controls to be created on runtime. Any setting initialization. Master pages and the settings. In this section, we do not have access to viewstate, postedvalues and neither the controls are initialized. In this section, the ASP.NET controls are fully loaded and you write UI manipulation logic or any other logic over here. If you have valuators on your page, you would like to check the same here. Its now time to send the output to the browser. If you would like to make some changes to the final HTML which is going out to the browser, you can enter your HTML logic here.

An SEI CMMI Level 3 Company

www.synechron.com

Page HttpModule

Unload PostRequestHandlerExecute

Page object is unloaded from the memory. Any logic you would like to inject after the handlers are executed. If you would like to save update some state variables like session variables. Before you end, if you want to update your cache.

HttpModule HttpModule

ReleaserequestState UpdateRequestCache

HttpModule

EndRequest

This is the last stage before your output is sent to the client browser.

An SEI CMMI Level 3 Company

www.synechron.com

Seq
1 2 3 4

Events
Init Load view state PostBackdata Load

Controls Initialized No

View state Available No

Form data Available No Not guaranteed Yes Yes

Not guaranteed Yes Not guaranteed Yes Yes Yes

5
6 7 8 9 10

Validate
Event Pre-render Save view state Render Unload

Yes
Yes Yes Yes Yes Yes

Yes
Yes Yes Yes Yes Yes

Yes
Yes Yes Yes Yes Yes

An SEI CMMI Level 3 Company

www.synechron.com

Вам также может понравиться