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
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:
In the Method Name combo box, select the event you want to implement: 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. |
|
|||||||
|