Overview
We'll cover the following:
- What is an event? - Briefly explains what an event is in the context of an ASP.NET page
-
So what’s cool about events? - Explains why events are cool and demonstrates using a simple example.
-
What order do events get executed? - Gives a breakdown of the order events are processed in a typical ASP.NET page.
-
Dissect and explain an event procedure. - Takes a closer look at some event procedure methods and explains what each component of the procedure does or cane be used for.
-
Some simple examples - demonstrate with some simple examples how to best utilize events in your page using Dreamweaver.
What order do events get executed?
Every time an ASP.NET page is viewed, many tasks are being performed behind
the scenes. Tasks are performed at key points ("events") of the
page's execution lifecycle.
The most common events are:
OnInit
The first event in our list to be raised is OnInit. When this event is raised,
all of the page's server controls are initialized with their property values.
PostBack values are not applied to the controls at this time.
OnLoad
The next event to be raised is OnLoad, which is the most important event
of them all as all the pages server controls will have their PostBack values
now.
PostBack Events
Next all the PostBack events are raised. By PostBack events, I am referring
to any event that is raised only on a PostBack such as button click events
and DropDownList item change events. These events are only raised when the
page view is the result of a PostBack. The order that these events are raised
can't be defined or relied upon; the only consistency with the order that
PostBack events are raised is that they are all raised between the OnLoad
and OnPreRender events.
OnPreRender
This event is raised just prior to the page or server control's html output
being written into the response stream that's sent to the client web browser.
This is last chance you have to make any modifications. By this point, all
the server controls on the page have the final data applied, for instance
any DataBound DropDownList's on the page are guaranteed to have their Items
collection populated by now.
OnUnload
This is the last event in our list to be raised and you should destroy any
un-managed objects and close any currently open database connection at this
point. You can't modify any controls on the page at this point as the response
stream has already been sent to the client web browser.
As each event of the page is raised it also automatically tells all its child
controls to raise their own implementation of the same event. In turn each
of those controls will tell its own child controls to do the same and so on
down the control tree till all controls have done so. Then execution flow
is passed back to the main page class to continue onto the next event and
the process is repeated for that event. You therefore know that when the pages
OnLoad event is raised so too are all it's server controls OnLoad events.
Dissect and explain an event procedure
ONow we will take look at a typical event procedure method for a button click.
First look at the event procedure in its entirety, then we will explain each
of the procedures components in more detail.
C#
void Button1_Click(object sender, System.EventArgs
e)
{
//your code here
}
VB
Sub Button1_Click(sender As Object, e As System.EventArgs)
'your code here
End Sub
Button control tag:
<asp:button OnClick="Button1_Click"
ID="Button1" runat="server" Text="OK" />
First of all we look at the method name "Button1_Click".The method
name could be anything you like but it's good practice to choose a name that
reflects what event it's going to be wired to; the best format is "ControlID_EventName",
where ControlID is the ID value of the control that raises the event and EventName
is the name of the event being handled. In the case of this example the event
is a click event for a button called Button1 so I used the method name of
"Button1_Click".Weeks later when I am looking at this code, I can
see instantly that this method is the event procedure code for Button1's click
event.
Next we look at the "sender" object, often defined as Src
in Dreamweaver's default code. This sender object is an object reference to
the Control or class instance that raised the event; in our example, this
will contain an object reference to the button that was clicked. Later we
will see how we can use this sender object to identify the control that raised
the event when the event procedure is wired to multiple controls.
Next we look at the "e" class object, often defined as E
in Dreamweaver's default code, which is an EventArgs class object that
can contain information about the event and the sender control, and known
as a Delegate or Event Delegate. In the case of a DataGrid's ItemCreated event
procedure which is raised each time a new DataGrid row is created, the e object
will contain a property called Item which itself contains a property
called ItemIndex. You can therefore find out precisely what the index
number of the newly created DataGrid row is by using e.Item.ItemIndex.
Next is the your code here section, I will leave what goes in this
section for you to decide, I'm sure you will work out what's meant to be in
this section given time to ponder!
The next component is the button tag itself or - to be more specific - the
OnClick attribute of the button tag. This should be set to the name of the
event procedure method, in the case of our example it set to:
OnClick="Button1_Click"