The Life Cycle of a Control

One key to understanding all controls is to understand the execution life cycle of a control. For example, when is the view state restored? What happens first, the Load event or the postback event notifications?

Table 6-1, which is based on the table included in the MSDN documentation, shows the life cycle of an ASP.NET control. One critical point to remember as you look at this table is that HTTP is a stateless protocol. The control life cycle is designed to create the illusion of state maintenance. Because you can modify the life cycle via exposed events, you can effect the illusion of state, for good or for ill.

Table 6-1 : Execution Life Cycle of an ASP.NET Control

Phase

Control Duties

Method or Event to Override

Initialize

Initializes settings needed during the lifetime of the incoming Web request

Init event (OnInit method)

Load view state

Customizes how view state is restored by overriding the

LoadViewState method LoadViewState method.

Process postback data

Processes incoming form data and updates properties. (Only controls that process postback data participate in this phase. The control must implement the IPostBackDataHan dler interface to handle this event.)

LoadPostData method

Load

Performs tasks

Load event (OnLoad method)

Table 6-1 : Execution Life Cycle of an ASP.NET Control

Phase

Control Duties

Method or Event to Override

common to all

requests, such as

opening database

connections. When

the Load event

takes place, server

controls are

created and

initialized, state

has been restored,

and form controls

reflect client-side

changes.

Send

Raises change

RaisePostDataChangedEvent

postback change notifications

events in response

method

to state changes between the

current and previous postbacks. As with the "process postback data" phase, this phase occurs only for controls that implement the IPostBackDataHan dler event.

Handle

Handles the client-

RaisePostBackEvent method

postback events

side event that caused the

postback and raises appropriate events on the server. As with the "process postback data" phase, this phase occurs only for controls that implement the IPostBackDataHan dler event.

Prerender

Performs any

PreRender event

changes required

(OnPreRender method)

before the control

is rendered.

Rendering a

control means

writing out the

HTML that will

create the control

on the client's

browser. Changes

to state made here

will be saved,

Table 6-1 : Execution Life Cycle of an ASP.NET Control

Phase

Control Duties

Method or Event to

Override

whereas changes

made in the

rendering phase

are not.

Save state

Saves the current

SaveViewState method

state of the control.

The ViewState

property of a

control is

automatically

persisted to a

string object after

this phase. The

string object is sent

to the client as a

hidden field in the

HTML that goes to

the client's

browser. A control

can override the

SaveViewState

method to change

the contents of the

ViewState

property, possibly

to create a more

efficient view state.

Render

Generates the

Render method

output to be

rendered to the

client.

Dispose

Performs any final

Dispose method

cleanup. Although

garbage collection

will eventually

recover any

unreferenced

objects, objects

that require

deterministic

freeing of

expensive

resources, such as

database

connections, can

be freed here.

Unload

Performs any final

Unload event (OnUnload

cleanup before the

method)

control is torn

down. Control

authors generally

perform cleanup in

Dispose and don't

handle this event.

When you're creating components, you might find yourself referring to Table 6-1 often. If you discover that an action doesn't have the desired effect, look carefully at where in the life cycle of the component you're taking an action that doesn't seem to work. In most cases, moving the action to a more appropriate event will eliminate the problem.

0 0

Post a comment