Flylib.com

Books Software

 
 
 

Chapter 8 -- Scripting Design-Time Controls

Chapter 8

Scripting Design-Time Controls

The design-time controls (DTCs) included with Microsoft Visual InterDev 6 each contain properties, events, and methods that developers can use to work with the DTCs in an application. These features provide a way of working with the DTC to receive data (by using properties), to track what users do with the DTC (by using events), and to cause the DTC to take certain actions (by using methods ).

When you use a DTC in a page, the DTC uses the Script Library to create an instance of a scripting object at run time. These underlying scripting objects implement the run-time behavior of the DTC. The interface and client-side actions of the DTC are usually implemented with a standard intrinsic HTML control, such as the Textbox or Button controls. Some DTCs, such as Recordset, are implemented with run-time features only, as there is no intrinsic HTML control for the client. This chapter deals with scripting DTCs in particular and does not explore using the database DTCs such as the Recordset. Those DTCs are covered in detail in Chapter 12, Chapter 13, and Chapter 14.

Choosing the Scripting Platform

You should exercise care in choosing the scripting platform for DTCs. If you set the scripting platform as Server, each time an event occurs in a DTC the event is processed on the server, resulting in a complete roundtrip from the browser to the server and back. This occurs because the Scripting Object Model wraps each page in an HTML form. When an event occurs, the form is posted to the server, processed , and then sent back to the browser. This processing takes time and should be carefully controlled.

If you set the scripting platform as Client, the events for a DTC are processed in the page completely in the browser. The result is faster response time, less network traffic, and less load on the server.

Often the best approach to choosing the scripting platform is to use both Server and Client modes for different DTCs. For example, you can have a page with a Textbox DTC and a Button DTC. If your database connections are all server-based, it might make sense to have the Textbox DTC use the Client scripting platform and the Button DTC use the Server scripting platform. This will allow the page to quickly process client events related to the Textbox, but let the server process any button-click events.

The Basics

To demonstrate how to script the DTCs, I created a simple Inventory database. This database contains the structure shown in Figure 8-1.

click to view at full size.

Figure 8-1. The Inventory database contains information for producing and maintaining inventory items and work orders.

I used Microsoft SQL Server for the database engine, but you can use Oracle, Microsoft Access, or any other database supported by Visual InterDev. The database access in this chapter will be simple. It will demonstrate how to script DTCs—not how to use the database features. You can learn more about scripting database features in Chapter 13 and Chapter 14.

A number of properties, methods , and events are shared across multiple DTCs. For example, almost every DTC will have show and hide methods that can be used to show or hide the DTC at run time. Some DTCs have a value property that you can use to extract data from the DTC or to place data in the DTC at run time.

Other DTCs have properties, methods, and events that are specific only to them. For instance, the Listbox and OptionGroup DTCs have an addItem method for adding items to the DTC at run time.

You can access the properties and methods of a DTC at run time by using the dot syntax. For example, to load a Listbox DTC that has an ID of ItemName , you might use the following code:

ItemName.addItem "Widget A"
ItemName.addItem "Widget B"
ItemName.addItem "Widget C"
ItemName.addItem "Widget D"

These commands add the Widget item names to ItemName .

You must call the methods for a DTC from the scripting platform it uses. For example, if you define a DTC and specify that it uses the Client scripting platform, the DTC's properties, methods, and events are available only in script code on the client. Conversely, if you define a DTC and specify that it uses the Server scripting platform, the DTC's properties, methods, and events are available only in ASP code running on the server.

To demonstrate how to call a method in the correct scripting platform, let's look at the following sample code:

{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}
<SCRIPT ID=serverEventHandlersVBS LANGUAGE=vbscript RUNAT=Server>
Sub thisPage_onenter()
    if thisPage.firstEntered then
        Listbox1.addItem "Widget A"
        Listbox1.addItem "Widget B"
        Listbox1.addItem "Widget C"
        Listbox1.addItem "Widget D"
    end if
End Sub
</SCRIPT>

This code runs whenever the page loads, whether the page is being accessed by a browser navigating to it or it is being entered by processing an HTML form. If the thisPage.firstEntered property is true, the user has navigated to the page and this code loads Listbox1 with data. The results are shown in Figure 8-2 below.

click to view at full size.

Figure 8-2. A server-side Listbox DTC.

You can also implement this code on the client, but you must change the event that contains the code to an event that will be fired in the browser. This code executes the same addItem statements, but this time they execute in the window_onload event in the browser.

<SCRIPT ID=clientEventHandlersVBS LANGUAGE=vbscript>
<!--
Sub window_onload
    ListBox1.addItem "Widget A"
    ListBox1.addItem "Widget B"
    ListBox1.addItem "Widget C"
    ListBox1.addItem "Widget D"
End Sub
-->
</SCRIPT>

Another issue you must correctly deal with involves the syntax of the various commands. Visual InterDev 6.0 implements the DTCs using the Script Library, which is composed of JScript routines. Service Pack 1 for Microsoft Visual Studio 6 will offer the option of using a set of COM components on the server in place of the JScript Script Library.

At least until Service Pack 1 is released, you must get used to using the correct case in your programming. JScript is case-sensitive while Microsoft Visual Basic is not. Since the DTCs are implemented using the JScript Script Library, the run-time executes all your code in JScript. This is true even if you are developing in VBScript (Visual Basic, Scripting Edition). For example, the following line of code will work:

if thisPage.firstEntered then

This line of code will fail:

if thisPage.firstEntered then

If you execute this last line of code, the following message will appear in the browser:

Microsoft VBScript runtime error '800a01a8' 
Object required: 'thispage' 
/VI6BookExamples/InventoryList.asp, line 13

This error is generated because the " p " in ' thispage ' is not capitalized. Capitalize the P , and the script works fine.