Published:
08 Sep 2002
|
Abstract
You've probably seen custom event arguments in use, probably in the DataGrid section of ASP.NET. Custom event arguments allow you to create a class that you pass along with your events that contain extra information in them that you may want. |
|
by . .
Feedback
|
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days):
18631/
42
|
|
|
Introduction |
Introduction
You've probably seen custom event arguments
in use, probably in the DataGrid section of ASP.NET. Custom event arguments
(from now on abbreviated to eventargs) allow you to create a class that you
pass along with your events that contain extra information in them that you
may want. The DataGridCommandEventArgs contains masses of information
about the DataGrid and lets you manipulate it through it. Although I won't be
showing you how to do that, this article will show you the basics of setting
up some custom eventargs and implementing them.
What are they?
Just incase you are unsure what I'm taking
about, custom eventargs are just a replacement (usually inherited from
EventArgs) for the normal EventArgs on event handlers.
eg. Instead of -
Sub MyEventHandler(ByVal
sender As
Object,
ByVal e
As
EventArgs) |
You would use -
Sub MyEventHandler(ByVal
sender As
Object,
ByVal e
As
CustomEventArgs) |
Why use them?
If you check out my
article on HTTPModules then you
will see that I use them for sending the name of the browser to the event
handler. If you have an event handler and you need to pass a lot of
information (eg. Information about a user - Name, Address, Username, password,
etc.) then this is the way.
The standard EventArgs can really only
be passed in as EventArgs.Empty ie. empty. However, the class is there
for you to be able to inherit from instead.
|
Creating the Arguments |
Creating the Arguments
Here is a sample class which has inherited
from EventArgs, it is the one in the HTTPModule article -
<Serializable()>
Public
Class
BrowserEventArgs : Inherits
EventArgs
Public BrowserName
As
String
Public
Sub
New(ByVal
BrowserID As
String)
BrowserName = BrowserID
End
Sub
End
Class |
|
Setting up and raising the event |
Setting up the event and raising it
Now you need to create a Delegate for
the event which will contain the custom event handler string.
Public
Delegate
Sub bdetEventHandler(ByVal
sender As
Object,
ByVal e
As BrowserEventArgs)
Public Event
bdet As
bdetEventHandler |
The bdetEventHandler is the Delegate
for bdet which basically means that the event bdet takes in two
parameters - an object and BrowserEventArgs object.
RaiseEvent bdet(Me,
New
BrowserEventArgs("Mozilla")) |
The event hander will then have to look like
-
Sub SampMod_bdet(ByVal
sender As
Object,
ByVal e
As
BrowserEventArgs)
.... |
Summary
This article was just a quick tutorial of how
to get up some custom event arguments. There is a lot more you can do with
these than I showed you here (as you can probably imagine) and there will be
more information on Delegates in the future.
|
|
|
User Comments
No comments posted yet.
|
Product Spotlight
|
|