Handling Errors

Controlling Objects with VBScript

Using script, you can control objects and respond to events. Before you can implement script, you need to identify the available objects on a Web page. For example, a number of intrinsic controls, such as push buttons or text boxes, are found on forms. Other potential objects include ActiveX controls and Java applets.

Identifying Objects

To use an object in client-side script, you must first create the object and then assign a name to it. You use this object name to create event procedures and to access the properties and methods of the object. The syntax for assigning names varies slightly for different types of objects.

click to view at full size.

Figure 9.2 Objects on a Web page that can be controlled using VBScript

Standard HTML Controls

To assign a name to a standard HTML control, you set the NAME attribute.

Example

This example assigns the name cmdValidateOrder to a Button control:
 <INPUT TYPE="BUTTON" NAME="cmdValidateOrder" VALUE="Validate Order"> 

ActiveX Controls

To assign a name to an ActiveX control, you set the ID attribute of the <OBJECT> tag.

Example

This example assigns the name lblOccupation to an ActiveX Label control:
 <OBJECT classid="clsid:99B42120-6EC7-11CF-A6C7-00AA00A47DD2" id=lblOccupation> </OBJECT> 

Java Applets

To assign a name to a Java applet, you set the NAME attribute of the <APPLET> tag.

Example

This example assigns the name myoutline to the Java Outline applet:
 <APPLET CODE=Outline.class NAME=myoutline HEIGHT=150 WIDTH=200> </APPLET> 

Adding Event Procedures

After you create and name the objects on a Web page, you can create event procedures for them. Script in the event procedure runs when the event occurs.

Common Events

Each object type recognizes a set of events. Common events include clicking a control, entering text in a control, and moving the cursor over a control.

ActiveX controls and Java applets support the same events on a Web page as they do in other environments, such as Visual Basic or Visual J++.

Creating an Event Procedure

There are a number of different ways to create an event procedure for an object.

Name the Procedure ObjectName_Event

If you name a procedure ObjectName_Event , the procedure runs automatically when the event for the object occurs. This is the same naming convention that you would use to define event procedures in Visual Basic.

Example

This example runs a procedure when the user clicks Button1 :
 Sub Button1_OnClick () End Sub 

Create a Separate <SCRIPT> Section

You can create a separate <SCRIPT> section that contains script to run for a specific event of a control.

Example

This example shows a script that runs when the Click event of the Calendar1 control occurs:
 <SCRIPT LANGUAGE="VBScript" FOR="Calendar1" EVENT="Click()"> <! MsgBox("hello") </SCRIPT> 

You can use this method in either JavaScript or VBScript to create event procedures for ActiveX controls and standard HTML controls.

Assign the Event Procedure When You Create the Object

In the HTML tag that creates an object, you can specify an event name and the procedure to be invoked when that event occurs. You must insert a <SCRIPT> section that includes the procedure declaration before the HTML tag that defines the object.

This method is useful if you want events from different objects to invoke the same procedure. You can use this method to assign event procedures for standard HTML controls.

Example

This example calls the ProcessOrder procedure when the user clicks the radio button.
 <SCRIPT LANGUAGE=VBScript> Sub ProcessOrder () ... End Sub </SCRIPT> <INPUT TYPE=RADIO NAME=RadioGroup onClick="ProcessOrder"> 
Both VBScript and JavaScript support this method.

Include Script in the HTML Tag

In the HTML tag that creates the object, you can specify an event name and the script to run when that event occurs. You can use this method to assign event procedures for standard HTML controls.

Example

This example displays the message "Hello World" when the user clicks the Hello button:
 <INPUT LANGUAGE="VBScript" TYPE=button Value="hello"  onClick="MsgBox 'Hello World'"> 


Microsoft Windows Architecture Training
Microsoft Windows Architecture for Developers Training Kit
ISBN: B00007FY9D
EAN: N/A
Year: 1998
Pages: 324

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net