Module 2: Using ActiveX Server Components

ActiveX Server components extend your scripting capabilities by providing a reusable means of gaining access to information. For example, the Database Access component enables scripts to query databases. Thus, whenever you want to query a database from a script, you can use the Database Access component; you need not write complex scripts to perform this task. You can call components from any script or programming language that supports Automation (ActiveX components are Automation servers). In this module, you will use ActiveX Server components to activate a sample Web site.

By now, you should be familiar with the basics of writing Active Server Pages. If not, complete Module 1 of this tutorial.


Lesson 1: Using the Ad Rotator Component

Web sites often sell or provide advertising space. To keep sites visually interesting and to display ads from several advertisers, you might want to cycle through the different advertisements. The Ad Rotator component simplifies the task of displaying each ad in turn and makes it easier to add new ads. In this lesson, you will create a script that calls the Ad Rotator component to rotate through four ads. Click the Show Me button below to see an example of the ad you are going to display, then click the button again to rotate to another ad.



Create the ad file

You will create a simple text file to tell the Ad Rotator component which ads to insert and for what percentage of time each ad should be displayed. We have already created a file containing ads from the Adventure Works sample Web site for you. To view it, use your text editor to open the file Adrot.txt in the Lessons directory (\Inetpub\ASPSamp\Tutorial\Lessons by default).

The first line of the file sets the script that will be called when a user clicks on an advertisement; in this case, Adredir.asp. This script enables you to track ad popularity. The following three lines establish the width, height, and border of the ad images.

redirect /aspsamp/advworks/adredir.asp
width 460
height 60
border 1

Next, the file contains ad data. For each ad, this includes the image to use, the URL to which to go when a user clicks the ad, the text associated with the image, and the percentage of time this ad is to be displayed:

/aspsamp/advworks/multimedia/images/ad_1.gif
http://www.microsoft.com
Astro Mt. Bike Company
20

By maintaining the ad information in a separate file, a different group at your company can update the Adrot.txt file without requiring you to update your ASP page. Different groups can maintain different files of ads for different parts of your site.

Create the script

  1. Use your text editor to open the file Ad.asp in the Lessons directory (\Inetpub\ASPSamp\Tutorial\Lessons by default).
  2. Search for the words "Tutorial Lesson: Ad Rotator." You will add your script here.
  3. Create an instance of the Ad Rotator component and assign it to the variable Ad by copying the following script command and pasting it into your text editor (after the comment):
    <% set Ad = Server.CreateObject("MSWC.Adrotator") %> 

    Assigning a component instance to a variable enables you to refer to a component later in a script.

  4. To fetch an ad from the file, use the GetAdvertisement method of the Ad Rotator component. Add the following command to your script:
    <% = Ad.GetAdvertisement("/aspsamp/tutorial/lessons/adrot.txt") %> 

    The GetAdvertisement method takes one parameter (the name of the file containing the ad information, in this case Adrot.txt). On the basis of this parameter, the method returns a fully formatted HTML <IMG> tag with the appropriate ad. The variable name you assigned to the Ad Rotator component instance, Ad, precedes the method, GetAdvertisement, and the path for the file Adrot.txt. The equal sign sends the value returned by the method (the actual ad) to the client browser.

  5. Save the file in text format and exit your text editor. Be sure your text editor does not replace the .asp file name extension.
  6. To verify that the ASP page you've created works (after which you can return to this Tutorial by clicking Back in your browser), point your browser to http://localhost/aspsamp/tutorial/lessons/ad.asp

On Your Own

The Adventure Works sample site also has an example of the Ad Rotator component.


Lesson 2: Using the Browser Capabilities component

Not all browsers support the rapidly expanding array of features available in the hypermedia world: Frames, background sounds, Java applets, and tables are examples of features that some browsers support and others do not. You can use the Browser Capabilities component to present content in formats that are appropriate for the capabilities of specific browsers. For example, if a browser does not support tables, the Browser Capabilities component can display an alternate form of text.

In this lesson, you will enhance the Ad Rotator script you created in Lesson 2. If a user's browser supports ActiveX controls, the user sees a of graphical images ads that appear one after another, with a variety of "fade-ins" and "fade-outs." If the browser does not support ActiveX controls, the user still sees the alternating series of ads that the Ad Rotator component displays. An example of such a browser-sensitive rotating ad appears below. (If your browser does not support this technology, you will see the same ad as in Lesson 1.)

Important   You must complete Lesson 1 before doing this lesson.



Create the Script

  1. Start your text editor and open the file Ad.asp in the Lessons directory (\Inetpub\ASPSamp\Tutorial\Lessons by default).

  2. Search for the words "Tutorial Lesson: Start Browser Capabilities." You will add your script below this comment.
  3. Create an instance of the Browser Capabilities component and assign it the variable OBJbrowser by copying the following script command from your browser and pasting it into Ad.asp. Be sure to insert the command above the <% Set Ad...%> statement:
    <% Set OBJbrowser = Server.CreateObject("MSWC.BrowserType") %>
  4. Use the VBScript If...Then statement to determine whether or not a client browser supports ActiveX controls. If it does, use the use the Ad Billboard control; if the browser does not support ActiveX Controls, use the Ad Rotator component on the server. To incorporate this logic, copy the following script and paste it after the <% Set OBJbrowser...%> statement:
    <% If OBJbrowser.ActiveXcontrols = "True" Then %> 
    <OBJECT HSPACE="10" WIDTH="460" HEIGHT="60" 
      CODEBASE="/aspsamp/advworks/controls/nboard.cab"  
      DATA="/aspsamp/advworks/controls/billboard.ods"> 
    </OBJECT> 
    <% Else %> 
    

    The Browser Capabilities component's ActiveXControls method determines whether the browser supports ActiveX controls.

    You use the <OBJECT> tag to insert an ActiveX control into an HTML page. The tag parameters specify the file from which the control reads data. In this example, this control reads compressed images from the Billboard.ods file.

  5. Search for the words "Tutorial Lesson - End Browser Capabilites." Replace the comment with the following script command to end the If...Then statement:
    <% End If %> 
  6. Save Ad.asp as a text file and exit your text editor. Be sure your text editor does not replace the .asp file name extension.
  7. To verify that the Active Server Page you've created works (after which you can return to this Tutorial by clicking Back in your browser), point your browser to http://localhost/aspsamp/tutorial/lessons/ad.asp
Note    The file Browscap.ini (found in C:\Winnt\system32\inetsrv\ASP\Cmpnts by default) contains the data necessary for the Browser Capabilities component to recognize a browser and its capabilities. You will need to maintain this file as new browsers are developed or if you are using browser-dependent features that are not listed in the default Browscap.ini file.

Lesson 3: Using the Database Access Component

The Database Access component uses Active Data Objects (ADO) to provide easy access to information stored in a database (or in another tabular data structure) that complies with the Open Database Connectivity (ODBC) standard. In this lesson, you will connect to a Microsoft® Access customer database and display a listing of its contents. You will learn how to extract data using the SQL SELECT statement and create an HTML table to display the results.



Identify the database

Before using a database with the Database Access component, you must identify the database in the ODBC application in Control Panel. In this example, you will use an Access database that is provided with the ASP sample Web site.

  1. At the computer running Windows NT Server, open Control Panel.

  2. Double-click the ODBC icon, and then click System DSN.

    There are two types of data sources: User, which is available only to you, and System, which is available to anyone using the computer. Data sources for use with the Web server need to be System.

  3. Click Add, choose the Microsoft Access Driver, and then click Finish.

  4. In the Data Source Name box, type AWTutorial, and then click Select. Select the \AspSamp\AdvWorks\AdvWorks.mdb file (in the Inetpub directory by default), and click OK.

  5. Click OK to close the dialog boxes.

Create the component instance

  1. Use your text editor to open the file Database.asp in the Lessons directory (\Inetpub\Aspsamp\Tutorial\Lessons by default).

  2. Search for the words "Tutorial Lesson - ADO Connection." You will replace this comment with your script.

  3. As always, you need to create an instance of the component in order to use it. Add the following code to your script:
    <% Set OBJdbConnection = Server.CreateObject("ADODB.Connection")  
  4. For the Database Access component, you also need to specify the ODBC data source (the database from which you want data) by opening a connection to the database. Add the following code to your script:
    OBJdbConnection.Open "AWTutorial" 
  5. Use the Database Access component Execute method to issue a SQL SELECT query (SQLQuery) to the database and store the returned records in a result set (RSCustomerList). Add the following code to your script below the OBJdbConnection.Open statement:

    SQLQuery = "SELECT * FROM Customers" 
    Set RScustomerList = OBJdbConnection.Execute(SQLQuery) 
    %>

    You could combine these two lines of script code by passing the literal SELECT string directly to the Execute method rather than assigning it to SQLQuery. When the query string is long, however, it makes the script easier to read if you assign the string to a variable name, such as SQLQuery, and then pass the variable name on to the Execute method.

Display the returned result set

You can think of the result set as a table whose structure is determined by the fields specified in the SQL SELECT query. Displaying the rows returned by the query, therefore, is as easy as performing a loop through the rows of the result set. In this example, the returned data is displayed in table rows.

  1. In Database.asp, find the words "Tutorial Lesson - Display ADO Data," and add the following VBScript Do...Loop statement:
    <% Do While Not RScustomerList.EOF %>
      <TR>
      <TD BGCOLOR="f7efde" ALIGN=CENTER> 
        <FONT STYLE="ARIAL NARROW" SIZE=1> 
          <%= RSCustomerList("CompanyName")%> 
        </FONT></TD>
      <TD BGCOLOR="f7efde" ALIGN=CENTER>
        <FONT STYLE="ARIAL NARROW" SIZE=1> 
          <%=RScustomerList("ContactLastName") & ", " %> 
          <%=RScustomerList("ContactFirstName") %> 
        </FONT></TD>
      <TD BGCOLOR="f7efde" ALIGN=CENTER>
        <FONT STYLE="ARIAL NARROW" SIZE=1>
        <A HREF="mailto:"> 
          <%=RScustomerList("ContactLastName")%> 
        </A></FONT></TD>
      <TD BGCOLOR="f7efde" ALIGN=CENTER>
        <FONT STYLE="ARIAL NARROW" SIZE=1> 
          <%=RScustomerList("City")%> 
        </FONT></TD>
      <TD BGCOLOR="f7efde" ALIGN=CENTER>
        <FONT STYLE="ARIAL NARROW" SIZE=1> 
          <%=RScustomerList("StateOrProvince")%> 
        </FONT></TD>
      </TR> 

    The Do...Loop statement repeats a block of statements while a condition is true. The repeated statements can be script commands or HTML text and tags. Thus, each time through the loop, you construct a table row (using HTML) and insert returned data (using script commands).

  2. To complete the loop, use the MoveNext method to move the row pointer for the result set down one row. Because this statement still falls within the Do...Loop statement, it is repeated until the end of the file is reached. Replace the comment with the words "Tutorial Lesson - Next Row" with the following code:
    <% 
    RScustomerList.MoveNext 
    Loop 
    %>
  3. Save Database.asp as a text file and exit your text editor. Be sure your text editor does not replace the .asp file name extension.

  4. Test the file in your browser by typing the following address: http://localhost/aspsamp/tutorial/lessons/database.asp

On your own

To see a more complete example of the Database Access component, look at the file Customer_Listing.asp in the Adventure Works sample site. Click the Show Script Code button to see the code that constructs the customer list.


© 1996 Microsoft Corporation. All rights reserved.