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.
Some simple examples
Now we will see some simple examples of events. The first is a very common
situation in which you want to check whether the page view is the result of
a PostBack or a fresh request. For this example we use the Page_Load event.
Example 1
C#
void Page_Load(object sender, System.EventArgs e)
{
if(Page.IsPostBack)
{
Response.Write("The page
view is the result of a PostBack!");
}
else
{
Response.Write("This
is a fresh request!");
}
}
VB
Sub Page_Load(sender As Object, e As System.EventArgs)
If Page.IsPostBack Then
Response.Write("The page
view is the result of a PostBack!")
Else
Response.Write("This
is a fresh request!")
End If
End Sub
There is no need to wire this event up to a tag (as in the previous example
with the button click) because here we're suing Page_Load which ASP.NET
wires up for you automatically.
Example 2
This example demonstrates the use of the click event of an ImageButton
to obtain the x and y coordinates of the position that the mouse pointer physically
clicked on the ImageButton.
C#
void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs
e)
{
Response.Write("X=" + e.X +
"<br>Y=" + e.Y);
}
VB
Sub ImageButton1_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs)
Response.Write("X=" & e.X
& "<br>Y=" & e.Y)
End Sub
ImageButton control tag:
<asp:imagebutton OnClick="ImageButton1_Click"
ID="ImageButton1" ImageUrl="button.gif" runat="server"
/>
This could be used to run different code depending on the coordinates where
the button was clicked . You may have noticed that the e Delegate is not of
the type System.EventArgs, this time but is actually an instance of the System.Web.UI.ImageClickEventArgs
class. The System.Web.UI.ImageClickEventArgs is an extended version of System.EventArgs
that contains two additional properties, X and Y which equate to the x and
y coordinates of the mouse pointer. You will discover lots of different Delegate
class types as you get deeper into ASP.NET and its events.
Example 3
In this example I will show you how you can use the previously discussed
"sender" object to identify the sender control. The event procedure
in this example is a method that is shared by more than one control. Imagine
a button control within a DataGrid where the button is repeated for each row
in the grid, resulting in many buttons. Here's the grid code:
<asp:DataGrid AllowPaging="false"
AutoGenerateColumns="false"
DataSource="<%# DataSet1.DefaultView %>"
runat="server">
<Columns>
<asp:BoundColumn DataField="UserName"
/>
<asp:TemplateColumn>
<ItemTemplate>
<asp:button OnClick="AllButtons_Click"
CommandArgument="<%# Container.ItemIndex %>" runat="server"
Text="OK" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
In the second DataGrid column we have button control:
<asp:button OnClick="AllButtons_Click" CommandArgument="<%#
Container.ItemIndex %>" runat="server" Text="OK"
/>
The OnClick event of the button is wired to an event procedure with a method
name of "AllButtons_Click" and the CommandArgument of the
button is set to equal the Container.ItemIndex property value. This means
the row index of the DataGrid as the Container object will evaluate to the
DataGrid. Each subsequent button in the grid will have its CommandArgument
value set to 0,1,2,3 and so on.
The "AllButtons_Click" event procedure method looks like:
C#
void AllButtons_Click(object sender, System.EventArgs
e)
{
System.Web.UI.WebControls.Button Btn;
Btn = (System.Web.UI.WebControls.Button)sender;
Response.Write("You clicked button
" + Btn.CommandArgument);
}
VB
Sub AllButtons_Click(sender As Object, e As System.EventArgs)
Dim Btn As System.Web.UI.WebControls.Button
= sender
Response.Write("You clicked button
" & Btn.CommandArgument)
End Sub
Now the "AllButtons_Click" method will be executed when clicking
on any of the button in the DataGrid column, and the code can identify what
button was clicked by using the "sender" object.
In the case of C#, the first 2 lines declare an object variable "Btn"
and then assign a value to that variable by casting the sender object to it.
The cast operation converts the sender object into a System.Web.UI.WebControls.Button,
and stores the new object reference in the "Btn" variable. The same
operation is performed in the VB version by the first line – only one
line because VB does all the casting from one type to another automatically.
The third line in the C# version performs the output.
Summary
In this tutorial I have explained in basic terms what an event is, why they
are so great and also outlined the order each of the common page events are
raised. I then broke down the individual components of an event procedure
method and gave some simple examples. By now I am hoping that you too now
think events are cool.