A web page is made various parts and objects. These include
the web page a visitor looks at, the controls on the web page, and the browser
that is displaying everything. All these objects behave certain ways when some
things happen. An event is an action that
occurs when something happens to an object and affects it.

To support events, the Visual Basic language provides the Event
keyword.

Event Creation

An event is declared like a pseudo-procedure. To actually declare an event, you use the Event keyword
with the following formula:

[modifier] EventName(Argument)

The modifier can be Public, Private, Protected,
or Friend. The Event keyword is required. It is followed by a name
for the event. If the event has arguments,
enter them in its parentheses.

To use an event, the object that causes it must initiate
it. To do this, the object must "raise the event". To assist you
with this, the Visual Basic language provides the RaiseEvent
operator. To use it, the formula to follow is:

RaiseEvent EventName(Argument(s))

You start with the RaiseEvent keyword followed by
the name of the event you would have declared previously. The name of the
event is followed by parentheses. Here is an example:

In reality, an event is something that happens to an object in
response to a change in behavior. To make this possible, an event uses a behavior
implemented by a delegate. This means that you must first create the
delegate. Once the delegate and the event have been declared, you can hook
up the delegate to the event. Then, when the appropriate action occurs, the
event fires. At that time, the procedure that implements the delegate
runs.

Before using an event, you must specify the procedure that
will carry the event. This procedure is referred to as a handler. To implement
this behavior, the event must return a value, and that value must be the
delegate that implements the desired behavior. Obviously you
must first have created a procedure. Here is an example:

To add a handler to the program, you use the AddHandler
operator with the following formula:

AddHandler EventName, AddressOf Procedure

The AddHandler and the AddressOf operators are
required. The EventName placeholder is used to specify the name of the
event that is being dealt with. The Procedure factor is the name of the
procedure that will implement the event. Here is an example:

The event we used in the previous sections did not take any
argument. Just like a delegate, an event can take an argument. The primary rule
to follow is that both its delegate and the procedure associated with it must
take the same type of event. The second rule is that, when raising the event,
you must pass an appropriate event to it. Here is an example:

Just like an event can take an argument, it can also take
more than one argument. The primary rules are the same as those for a single
parameter. You just have to remember that you are dealing with more than one
argument. Here is an example:

Instead of a parameter of a primitive type, you can create
an event that takes a class as argument.

Events and Web Controls

Introduction

As mentioned already, a web page presents various things,
such as controls, to a visitor. These controls regularly send messages to the
server to do something. These messages are similar to human messages and must be processed appropriately.

Events of objects on a web page are implements through the
concepts of delegates and events as reviewed previously. The most common events have already been
created for the web controls that appear on a web page, so much that you will hardly need to define new events. Most of what you will do consists of implementing the desired behavior when a particular
event fires.
To start, you should know what events are available, when they work, how they work,
and what they produce.

To process a message, it (the message) must provide at least two pieces of information:
What caused the
message and what type of message is it? Both values are passed as the arguments to the event.
Since all web controls used on a web page are based on the Object
class, the first argument must be an Object type and represents the
control that sent the message. Therefore, the procedure of an event would
start as follows:

The access modifier specifies the level of access that the
procedure can afford. In most cases, it will be Protected.

As mentioned already, each control sends its own messages when necessary. Based on this, some messages are unique to some controls according to their roles. Some other messages are common to various controls, as they tend to provide similar actions.

As it happens, in order to perform their intended action(s),
some messages do not require much information. A message that does not need
particular information is carried by a class named EventArgs. In the
event implementation, an EventArgs argument is passed as the second
parameter.T herefore, the procedure of an event would start as follows:

Although there are different means of implementing an event, there are
various ways you can initiate its coding. If you are writing your code from a
text editor, in the head section, create a <script> tag, inside the tag
section, add the necessary code of the event. This would be done as follows: