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.
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.
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.
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.
<% = 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.
The Adventure Works sample site also has an example of the Ad Rotator 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.
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") %>
<% 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.
<% End If %>
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.
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.
<% Set OBJdbConnection = Server.CreateObject("ADODB.Connection")
OBJdbConnection.Open "AWTutorial"
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.
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.
<% 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).
<% RScustomerList.MoveNext Loop %>
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.