Documente Academic
Documente Profesional
Documente Cultură
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 step, 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. Additionally, during the
start step, the page's UICulture property is set.
Page
initialization
During page initialization, controls on the page are available and each
control's UniqueID property is set. Any themes are also applied to the
page. 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
Validation
Postback
event
handling
Rendering
Before rendering, view state is saved for the page and all controls. During
the rendering phase, the page calls the Render method for each control,
providing a text writer that writes its output to the OutputStream of the
page's Responseproperty.
Unload
Unload is called 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 any cleanup is performed.
Life-cycle Events
Within each stage of the life cycle of a page, the page raises events that you
can handle to run your own code. For control events, you bind the event
handler to the event, either declaratively using attributes such as onclick,
or in code.
Pages also support automatic event wire-up, meaning that ASP.NET looks for
methods with particular names and automatically runs those methods when
certain events are raised. If the AutoEventWireup attribute of
the @ Page directive is set to true (or if it is not defined, since by default it
is true), page events are automatically bound to methods that use the
naming convention of Page_event, such as Page_Load and Page_Init. For
more information on automatic event wire-up, see ASP.NET Web Server
Control Event Model.
The following table lists the page life-cycle events that you will use most
frequently. There are more events than those listed; however, they are not
used for most page processing scenarios. Instead, they are primarily used
by server controls on the ASP.NET Web page to initialize and render
themselves. If you want to write your own ASP.NET server controls, you
need to understand more about these stages. For information about creating
custom controls, see Developing Custom ASP.NET Server Controls.
Page Event
Typical Use
PreInit
Init
InitComplete
PreLoad
Load
Control events
Use this event for tasks that require that all other
controls on the page be loaded.
PreRender
SaveStateComplete Before this event occurs, ViewState has been saved for
the page and for all controls. Any changes to the page
or controls at this point will be ignored.
Use this event perform tasks that require view state to
be saved, but that do not make any changes to
controls.
Render
This event occurs for each control and then for the
page. In controls, use this event to do final cleanup for
specific controls, such as closing control-specific
database connections.
For the page itself, use this event to do final cleanup
work, such as closing open files and database
connections, or finishing up logging or other requestspecific tasks.
Note:
During the unload stage, the page and its controls
have been rendered, so you cannot make further
changes to the response stream. If you attempt to call
a method such as the Response.Write method, the
page will throw an exception.
ASP.NET handles the task of capturing, transmitting, and interpreting the event.
When you create event handlers in an ASP.NET Web page, you can typically do so
without thinking about how the event information is captured and made available to
your code. Instead, you can create event handlers in much the same way you
would in a traditional client form. However, there are some aspects of event
handling in ASP.NET Web pages that you should be aware of.
Event Arguments
Server-based ASP.NET page and control events follow a standard .NET Framework
pattern for event-handler methods. All events pass two arguments: an object
representing the object that raised the event, and an event object containing any
event-specific information. The second argument is usually of type EventArgs, but
for some controls is of a type specific to that control. For example, for
an ImageButton Web server control, the second argument is of
typeImageClickEventArgs, which includes information about the coordinates where
the user has clicked.
Note:
Events for the page (for example, the page's Load event) can accept the standard two argument
passed in these arguments.
Note:
If the browser supports it, validation controls can check user input using client script, without a r
server. For details, see Validating User Input in ASP.NET Web Pages.
After a page has been posted back, the page's initialization events
(Page_Init and Page_Load) are raised, and then control events are processed.
You should not create application logic that relies on the change events being raised
in a specific order unless you have detailed knowledge of page event processing.
For details, see ASP.NET Page Life Cycle Overview.
If it useful for your application, you can specify that change events cause the page
to post. Web server controls that support a change event include
anAutoPostBack property. When this property is true, the control's change event
causes the page to post immediately, without waiting for a click event. For
example, by default, a CheckBox control's CheckedChanged event does not cause
the page to be submitted. However, if you set the control's AutoPostBackproperty
to true, as soon as a user clicks the check box, the page is sent to the server for
processing.
Note:
For the AutoPostBack property to work properly, the user's browser must be set to allow script
default in most cases. However, some users disable scripting for security reasons. For details, se
ASP.NET Web Pages.
Forwarded Events
Web server controls such as the Repeater, DataList, GridView, FormView,
and DetailsView controls can contain button controls that themselves raise events.
For example, each row in a GridView control can contain one or more buttons
created dynamically by templates.
Rather than each button raising an event individually, events from the nested
controls are forwarded to the container control. The container in turn raises a
generic ItemCommand event with parameters that allow you to discover which
individual control raised the original event. By responding to this single event, you
can avoid having to write individual event handlers for child controls.
The ItemCommand event includes the two standard event arguments, an object
referencing the source of the event and an event object containing event-specific
information.
Note:
The GridView, DataList, and other data controls support additional events, such
as EditCommand, DeleteCommand, and UpdateCommand, that are special cases of forward
With buttons, you can use the CommandArgument property to pass a userspecified string to the event handler to help you identify what button raised the
event. For example, in a DataList control, buttons raise the ItemCommand event.
You can set the CommandArgument property of each button to a different
valueperhaps one button's value is "ShowDetails" and another button's value is
"AddToShoppingCart"and then capture those values in the event handler later.
Note:
Page event-handling methods do not require any arguments.
If you prefer, you can bind handlers to events explicitly. The automatic binding of
page events based on the method naming convention is controlled by a page
property named AutoEventWireup. By default, for C#, this property is set
to true, and ASP.NET performs the automatic lookup and binding described earlier.
Alternatively, you can set this property to false by adding the
attribute AutoEventWireup=false in the @ Page directive. You can then create
methods with any name and bind them to page events explicitly.
By default, for Visual Basic, this property is set to false. In Visual Basic, handlers
are bound to events by using the Handles keyword. This keyword is inserted
automatically by Visual Studio as part of the method that you create when you
select a page event from the drop-down box. The following example illustrates use
of the Handles keyword:
Visual Basic
If you include explicit binding for page events, be sure that the AutoEventWireup property is se
the method is not called twice.
Explicit Binding for Dynamic Controls
If you create controls by declaring them in markup, you can bind events to
methods using an attribute (for example, onclick) or in Visual Basic, by using
theHandles keyword. If you create controls dynamically (in code), you cannot use
either of these methods, because the compiler does not have a reference to the
control at compilation time.
In that case, you must use explicit event binding. In Visual Basic, you can use
the AddHandler statement to bind an event in a dynamically created control to an
existing method. In C#, you create a delegate and associate it with the control's
event. The following code example shows how you can bind a method
namedButtonClick to a button's Click event:
Visual Basic
b.Text = "Click"
AddHandler b.Click, AddressOf ButtonClick
Placeholder1.Controls.Add(b)
C#
Application events are raised for all requests to an application. For example,
the BeginRequest event of the HttpApplication object
(Application_BeginRequest) is raised when any ASP.NET Web page or
XML Web service in your application is requested. This event allows you to
initialize resources that will be used for each request to the application. A
corresponding event, the EndRequest event of the HttpApplication object
(Application_EndRequest), provides you with an opportunity to close or
otherwise dispose of resources used for the request.