Home

Events

 

Events Fundamentals

 

Introduction

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] Event Name(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.

Here is an example that declares an event:

<script language="vbscript" type="text/vbsscript" runat="server">

Private Event Evidence()

</script>

Raising an Event

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:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

Private Event Evidence()

</script>

<title>Exercise</title>

</head>
<body>

<%
    RaiseEvent Evidence()
%>

</body>
</html>

Events and Delegates

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:

<script language="vbscript" type="text/vbsscript" runat="server">

Private Sub ShowMessage()
    Response.Write("Welcome to our web site")
End Sub

</script>

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:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

Delegate Sub Messenger()
    
Private Event Evidence As Messenger

Private Sub ShowMessage()
    Response.Write("Welcome to our web site")
End Sub

</script>

<title>Exercise</title>

</head>
<body>

<%
    AddHandler Evidence, AddressOf ShowMessage
%>

</body>
</html>

Raising an Event

After adding a handler to the event, it is ready but you must launch its action. To do this, you can use the RaiseEvent operator with the following formula:

RaiseEvent EventName()

The RaiseEvent operator is required. The EventName placeholder is used to specify the name of the event, and it must be followed by parentheses. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

Delegate Sub Messenger()
Private Event Evidence As Messenger

Private Sub ShowMessage()
    Response.Write("Welcome to our web site")
End Sub

</script>

<title>Exercise</title>

</head>
<body>

<%
    AddHandler Evidence, AddressOf ShowMessage

    RaiseEvent Evidence()
%>

</body>
</html>

This would produce:

Event

A Parameterized Event

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:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

Delegate Sub Messenger(ByVal Msg As String)
Private Event Evidence As Messenger

Private Sub ShowMessage(ByVal ToShow As String)
    Response.Write(ToShow)
End Sub

</script>

<title>Exercise</title>

</head>
<body>

<%
    AddHandler Evidence, AddressOf ShowMessage

    RaiseEvent Evidence("This is one of the best things today.")
%>

</body>
</html>

This would produce:

Event

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:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

Delegate Sub Messenger(ByVal Originator As String, ByVal Msg As String)
Private Event Evidence As Messenger

Private Sub ShowMessage(ByVal Sender As String, ByVal ToShow As String)
    Response.Write(Sender & ": " & ToShow)
End Sub

</script>

<title>Exercise</title>

</head>
<body>

<%
    AddHandler Evidence, AddressOf ShowMessage

    RaiseEvent Evidence("Message form the Accounting Department", _
                        "Remember to do your time sheet")
%>

</body>
</html>

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:

AccessModifier Sub Control_Event(ByVal Sender As Object, ...)...

End Sub

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:

AccessModifier Sub Control_Event(ByVal sender As Object, ByVal e As EventArgs) ...

End Sub

Not all events are of type EventArgs. Eventually, we will see different types of events and we will analyze them.

When creating an event, you must specify what web control will fire the event. To support this, the procedure uses an operator named Handles. The formula to follow is:

AccessModifier Sub Control_Event(ByVal sender As Object, _
				 ByVal e As EventArgs) Handles ...

End Sub

After the Handles keyword, enter the name of the control, followed by a period, and followed by the event. Here is an example:

AccessModifier Sub Control_Event(ByVal sender As Object, _
			       ByVal e As EventArgs) Handles SomeTextBox.TextChanged

End Sub

Event Implementation

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:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

AccessModifier Sub Control_Event(ByVal sender As Object, _
			       ByVal e As EventArgs) Handles SomeTextBox.TextChanged

End Sub

</script>

<title>Exercise</title>

</head>
<body>

</body>
</html>

Between the starting Sub line and the End Sub line, write your code as you see fit.

If you are using either Microsoft Visual Studio or Microsoft Visual Web Developer:

  • On the web form, you can double-click the web control. This would initiate its default event
  • Click either the body of the form or a control on it. Then, on the Properties window, click the Events button Events, and double-click the name of the event you want to use

Events

  • Right-click the form and clicking View Code. Then, in the Class Name combo box, select the name of the control:

Class Name

In the Method Name combo box, select the event you want to implement:

Method Name

Any of these actions would initiate the event in the Code Editor. The cursor would be positioned in the body of the event, ready to receive your instructions.

 
 
   
 

Home Copyright © 2009-2013 FunctionX, Inc.