Event Handling
When you perform an action with an object, the object in
turn
raises events in the application. Clicking a button, for example,
generates an event. Not all events are triggered by
user
actions,
however. Changes in the environmentsuch as the
arrival of an email message, modifications to a file, change in
time, and completion of program executionalso
trigger events.
Handling Events by Overriding the Virtual, Protected Method of
the Base Class
When an ASP.NET page is dynamically compiled, it inherits from
the
Page
class.
The following example
demonstrates
event handling by overriding
the virtual, protected method of the base class:
-
Add a new text file to project
315C02
and
name
it
Example2_3.aspx
.
-
Switch to the HTML
view and add the following code to the
Example2_3.aspx
file:
<%@ Page Language="c#"%>
<html><body>
<script runat="server">
protected override void OnLoad(EventArgs e)
{
Response.Write(
"Message from OnLoad() method.<br>");
base.OnLoad(e);
}
protected override void OnInit(EventArgs e)
{
Response.Write(
"Message from OnInit() method.<br>");
base.OnInit(e);
}
protected override void OnPreRender(EventArgs e)
{
Response.Write(
"Message from OnPreRender() method.<br>");
base.OnPreRender(e);
}
</script>
<hr></body>
</html>
-
View
Example2_3.aspx
in your browser.
When an ASP.NET Page object is created, it invokes the
OnInit()
,
OnLoad()
, and
OnPreRender()
methods
to raise the
Init
,
Load
, and
PreRender
events, respectively, in that order. You can
write custom code in these methods in response to the specific
events.
The predefined event handlers
OnInit()
and
OnLoad()
also bootstrap the event handling process for
events (
Init
,
Load
, and so on) of the
Page
class. However, you need not program this
functionality again because it is already programmed in the base
class. But to ensure that the base class version of an overridden
method is also invoked, you need to explicitly call the base class
version of a method by using the
base.
MemberName
syntax.
Handling Events by Attaching a Delegate
A
delegate
is a special class whose object is capable of
storing references to methods with a particular prototype. These
methods are nothing but the event handlers that need to be invoked
in response to an event.
Using delegates to handle events is far more flexible because of
the following reasons:
-
Delegates allow you to attach a single event handler to several
events. This technique eliminates extra coding efforts when you
have to take similar actions when different events occur.
-
Delegates allow you to dynamically add and remove event handlers
for an event. You can also associate multiple event handlers for an
event.
-
The class that handles the event is not required to derive from
the class that publishes an event.
The following example uses delegates to display messages when
various events for a
Page
object are raised.
-
Add a new text file to project
315C02
and name it
Example2_4.aspx
.
-
Switch to the HTML
view and add the following code to the
Example2_4.aspx
file:
<%@ Page Language="c#"%>
<html><body>
<script runat="server">
protected void Example2_4_Load(
Object o, EventArgs e)
{
Response.Write(
"Message from Load event handler.<br>");
}
protected void Example2_4_Init(
Object o, EventArgs e)
{
Response.Write(
"Message from Init event handler.<br>");
}
protected void Example2_4_PreRender(
Object o, EventArgs e)
{
Response.Write(
"Message from PreRender event handler.<br>");
}
protected override void OnInit(EventArgs e)
{
this.Load += new EventHandler(
Example2_4_Load);
this.PreRender += new EventHandler(
Example2_4_PreRender);
this.Init += new EventHandler(
Example2_4_Init);
base.OnInit(e);
}
</script>
<hr></body>
</html>
-
View
Example2_4.aspx
in your browser. When an ASP.NET page is
loaded for the first time, the
Init
,
Load
, and
PreRender
events occur in order.
Why use the
OnInit()
method in code? You do so because
writing code in the
OnInit()
method ensures that the code
will be executed before any other processing on the page.
The crux of the event-handling technique used in the previous
example lies in the first three lines of the
OnInit()
method. Each of these three lines works in the same way. Consider
the first line:
this.Load += new EventHandler(Example2_4_Load);
This looks like a complex statement, but if you examine it
carefully
, you can see that there are three
parts
to it with the
following roles:
-
The
Load
event is raised when an ASP.NET page is
loaded
A set of event handlers can be
attached to an event, and when the event is
fired
, it invokes all
the attached event handlers. An event handler can be attached to
the
Load
event only through its delegate object. The
this
keyword qualifies the
Load
event for the
current instance of the Web page.
-
Example2_4_Load
is the name of the method
responding to the
Load
event
When a method name is used without any argument list, it works as a
reference to the actual method definition.
-
The delegate type of the
Load
event is
EventHandler
You can add event
handlers to a
Load
event only by adding new instances of
the delegate to it. In the Visual Studio .NET documentation, the
definition of the
EventHandler
delegate looks like
this:
public delegate void EventHandler(
object sender, EventArgs e);
This means that the
EventHandler
delegate is capable of
storing references to any method that has a
void
return
type and that accepts two arguments: the first one of type
System.Object
and the other one of type
EventArgs
. The signature of the
Example2_4_Load()
method matches the criteria of this delegate, and hence a reference
to the
Example2_4_Load()
method can be stored in an
instance of a delegate of type
EventHandler
.
When you have an instance of the
EventHandler
delegate,
you can attach it to the event by using the
+=
operator.
The
+=
operator ensures that, if any event handlers are
already attached to this event by the base class, they
remain
in
the list.
At a later stage, when the
Load
event is raised, the
Example2_4_Load()
method is invoked through its reference
that is
maintained
by the delegate object.
Handling Events of the
Page
Class Through
AutoEventWireup
ASP.NET supports an additional mechanism for handling events
raised by the
Page
class. This technique is called as
AutoEventWireup
.
AutoEventWireup
relies on using specific
names
for the
event handlers of
Page
class events. The name has to be of
the type
Page_
EventName
()
.
For example, when handling the
Init
,
Load
, and
PreRender
events, the name of the event handlers should be
Page_Init()
,
Page_Load()
, and
Page_PreRender()
, respectively.
Event handling through
AutoEventWireup
works only when
the
AutoEventWireup
attribute of the
Page
directive is
true
. The
AutoEventWireup
attribute
is
true
by default, so if you don't specify this attribute
in the
Page
directive, it's assumed
true
by
ASP.NET.
The following example demonstrates the use of predefined event
handlers to handle various events of the
Page
class:
-
Add a new text file to project
315C02
and name it
Example2_5.aspx
.
-
Switch to the HTML
view and add the following code to the
Example2_5.aspx
file:
<!--Example2_5.aspx-->
<%@ Page Language="c#"%>
<html><body>
<script runat="server">
protected void Page_Load(Object o, EventArgs e)
{
Response.Write(
"Message from Load event handler.<br>");
}
protected void Page_Init(Object o, EventArgs e)
{
Response.Write(
"Message from Init event handler.<br>");
}
protected void Page_PreRender(
Object o, EventArgs e)
{
Response.Write(
"Message from PreRender event handler.<br>");
}
</script>
<hr></body>
</html>
-
View
Example2_5.aspx
in your browser. You should see results
similar to
Example2_4.aspx
.
AutoEventWireup
isn't as flexible as event handling
through delegates because the page event handlers are required to
have specific names. Therefore,
AutoEventWireup
is set to
false
when creating Web forms using Visual Studio
.NET.
|