home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / MS_DEV / VID / SERVER / ASF / DATA.Z / atumd2.asp < prev    next >
Text File  |  1996-10-22  |  17KB  |  252 lines

  1. <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
  2.  
  3. <html>
  4. <HEAD>
  5. <title>Module 2: Using ActiveX Server Components</title>
  6. </HEAD>
  7. <BODY BGCOLOR="#FFFFFF"><FONT FACE="ARIAL,HELVETICA">
  8. <h1><a name="Module2">Module 2:</a><a name="ChapTop"> Using ActiveX Server Components</a></h1>
  9.  
  10. <p>ActiveX Server <strong>components</strong>  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 
  11. programming language that supports Automation (ActiveX components are Automation servers). In this module, you will use 
  12. ActiveX Server components to activate a sample Web site.
  13.  
  14. <p>By now, you should be familiar with the basics of writing Active Server Pages. If not, complete <a href="atumd1.asp">Module 1</a> of this tutorial. 
  15.  
  16. <hr>
  17. <h2><a name="UsingtheAdRotatorComponent">Lesson 1: Using the Ad Rotator Component</a></h2>
  18.  
  19. <p>Web sites often sell or provide advertising space. To keep sites visually interesting and to display ads from several advertisers, you might 
  20. want to cycle through the different advertisements. The Ad Rotator component simplifies the task of displaying each ad in turn and makes 
  21. 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.
  22.  
  23. <hr>
  24. <!--#include file="script4.asp" -->
  25. <hr>
  26.  
  27. <h3><A NAME="Createtheadfile">Create the ad file</A></h3>
  28.  
  29. <p>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 
  30. containing ads from the Adventure Works sample Web site for you. To view it, use your text editor to 
  31. open the file Adrot.txt in the Lessons directory (\Inetpub\ASPSamp\Tutorial\Lessons by default). 
  32.  
  33. <p>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.
  34.  
  35. <FONT FACE="COURIER"><pre>redirect /aspsamp/advworks/adredir.asp
  36. width 460
  37. height 60
  38. border 1</pre></FONT>
  39.  
  40. <p>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:
  41.  
  42. <FONT FACE="COURIER"><pre>/aspsamp/advworks/multimedia/images/ad_1.gif
  43. http://www.microsoft.com
  44. Astro Mt. Bike Company
  45. 20</pre></FONT>
  46.  
  47. <p>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. 
  48.  
  49. <h3><A NAME="Createthescript">Create the script</A></h3>
  50.  
  51. <ol>
  52. <li>Use your text editor to open the file Ad.asp in the Lessons directory (\Inetpub\ASPSamp\Tutorial\Lessons by default).
  53.  
  54. <li>Search for the words "Tutorial Lesson: Ad Rotator." You will add your script here.
  55.  
  56. <li>Create an instance of the Ad Rotator component and assign it to the variable <FONT FACE="COURIER"><code>Ad</code></FONT> by copying the following script command and pasting it into your text editor (after the 
  57. comment): <FONT FACE="COURIER"><pre><% set Ad = Server.CreateObject("MSWC.Adrotator") %> </pre></FONT>
  58.  
  59. <p>Assigning a component instance to a variable enables you to refer to a component later in a script.
  60.  
  61. <li>To fetch an ad from the file, use the <b>GetAdvertisement</b> method of the Ad Rotator component. Add the following command to your script: <FONT FACE="COURIER"><pre><% = Ad.GetAdvertisement("/aspsamp/tutorial/lessons/adrot.txt") %> </pre></FONT>
  62.  
  63. <p>The <b>GetAdvertisement</b> 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 <FONT FACE="COURIER"><code><IMG></code></FONT> tag
  64. with the appropriate ad. The variable name you assigned to the Ad Rotator component instance, <b>Ad</b>, precedes the method, 
  65. <b>GetAdvertisement</b>, and the path for the file Adrot.txt. The equal sign sends the value returned by the method (the actual ad) to  
  66. the client browser. </p>
  67.  
  68. <li>Save the file in text format and exit your text editor. Be sure your text editor does not replace the .asp file name extension.
  69.  
  70. <li>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
  71. <a href="http://localhost/aspsamp/tutorial/lessons/ad.asp">http://localhost/aspsamp/tutorial/lessons/ad.asp</a> </li>
  72. </ol>
  73. <h3><A NAME="OnYourOwn">On Your Own</A></h3>
  74.  
  75. <p>The Adventure Works sample site also has an example of the <a href="http://localhost/advworks/excursions/default.asp">Ad Rotator component</a>. 
  76.  
  77. <hr>
  78. <h2><a name="UsingtheBrowserCapabilitiesComponent">Lesson 2: Using the Browser Capabilities component</a></h2>
  79.  
  80. <p>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. 
  81.  
  82. <p>In this lesson, you will enhance the Ad Rotator script you created in Lesson 2. If a user's browser 
  83. 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 <a href="#UsingtheAdRotatorComponent">Lesson 1</a>.)
  84.  
  85. <p><font color="#FF0000"><strong>Important</strong></font>   You must complete <a href="#UsingtheAdRotatorComponent">Lesson 1</a> before doing this lesson. 
  86.  
  87. <hr>
  88. <!--#include file="script5.asp" -->
  89. <hr>
  90.  
  91. <h3><A NAME="CreatetheScript">Create the Script </A></h3>
  92.  
  93. <ol>
  94. <li>Start your text editor and open the file Ad.asp in the Lessons directory (\Inetpub\ASPSamp\Tutorial\Lessons by default).<br>
  95. <br>
  96.  
  97. <li>Search for the words "Tutorial Lesson: Start Browser Capabilities." You will add your script below this comment.
  98.  
  99. <li>Create an instance of the Browser Capabilities component and assign it the variable <FONT FACE="COURIER"><code>OBJbrowser</code></FONT> by copying the following script command from your browser and pasting it into 
  100. Ad.asp. Be sure to insert the command above the <FONT FACE="COURIER"><code><% Set Ad...%> </code></FONT>statement: <FONT FACE="COURIER"><pre><% Set OBJbrowser = Server.CreateObject("MSWC.BrowserType") %></pre></FONT>
  101.  
  102. <li>Use the VBScript <strong>If...Then</strong> 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 <FONT FACE="COURIER"><code><% Set OBJbrowser...%></code></FONT> 
  103. statement: <FONT FACE="COURIER"><pre><% If OBJbrowser.ActiveXcontrols = "True" Then %> 
  104. <OBJECT HSPACE="10" WIDTH="460" HEIGHT="60" 
  105.   CODEBASE="/aspsamp/advworks/controls/nboard.cab"  
  106.   DATA="/aspsamp/advworks/controls/billboard.ods"> 
  107. </OBJECT> 
  108. <% Else %> 
  109. </pre></FONT>
  110.  
  111. <p>The Browser Capabilities component's <strong>ActiveXControls</strong> method determines whether the browser supports ActiveX controls. </p>
  112. <p>You use the <FONT FACE="COURIER"><code><OBJECT></code></FONT> tag to insert an ActiveX control into an HTML page. The tag parameters specify the file from which the 
  113. control reads data. In this example, this control reads compressed images from the Billboard.ods file. </p>
  114.  
  115. <li>Search for the words "Tutorial Lesson - End Browser Capabilites." Replace the comment with the following script command to end the 
  116. <strong>If...Then</strong> statement: <FONT FACE="COURIER"><pre><% End If %> </pre></FONT>
  117.  
  118. <li>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.
  119. <br>
  120.  
  121. <li>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 <a href="http://localhost/aspsamp/tutorial/lessons/ad.asp">http://localhost/aspsamp/tutorial/lessons/ad.asp</a> 
  122.  
  123. </ol>
  124. <b>Note</b>    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. 
  125.  
  126. <hr>
  127. <h2><a name="UsingtheDatabaseAccessComponent">Lesson 3: Using the Database Access Component </a></h2>
  128. <p>The Database Access component uses Active Data Objects (ADO) to provide easy access to information stored in a database (or in 
  129. another tabular data structure) that complies with the Open Database Connectivity (ODBC) standard. In this lesson, you will connect to a 
  130. Microsoft® Access customer database and display a listing of its contents. You will learn how to extract data using the SQL <strong>SELECT</strong> 
  131. statement and create an HTML table to display the results.</p>
  132. <hr>
  133. <!--#include file="script6.asp" -->
  134. <hr>
  135.  
  136. <h3><A NAME="Identifythedatabase">Identify the database</A></h3>
  137. <p>Before using a database with the Database Access component, you must identify the database in the ODBC application in Control Panel. 
  138. In this example, you will use an Access database that is provided with the ASP sample Web site.</p>
  139.  
  140. <ol>
  141. <li>At the computer running Windows NT Server, open <strong>Control Panel</strong>. <br>
  142.  
  143. <br>
  144. </li>
  145. <li>Double-click the ODBC icon, and then click <strong>System DSN</strong>. <br>
  146. <br>
  147. There are two types of data sources: <strong>User</strong>, which is available only to you, and <strong>System</strong>, which is available to anyone using the 
  148. computer. Data sources for use with the Web server need to be <strong>System</strong>.<br>
  149. <br>
  150.  
  151. <li>Click <strong>Add</strong>, choose the <strong>Microsoft Access Driver</strong>, and then click <strong>Finish</strong>. <br>
  152. <br>
  153. </li>
  154. <li>In the <strong>Data Source Name </strong>box, type <b>AWTutorial</b>, and then click <strong>Select</strong>. Select the \AspSamp\AdvWorks\AdvWorks.mdb file 
  155. (in the Inetpub directory by default), and click <strong>OK</strong>. <br>
  156. <br>
  157. </li>
  158. <li>Click <Strong>OK </Strong> to close the dialog boxes. </li>
  159. </ol>
  160. <h3><A NAME="Createthecomponentinstance">Create the component instance</A></h3>
  161. <ol>
  162.  
  163. <li>Use your text editor to open the file Database.asp in the Lessons directory (\Inetpub\Aspsamp\Tutorial\Lessons by default).<br>
  164. <br>
  165.  
  166. <li>Search for the words "Tutorial Lesson - ADO Connection." You will replace this comment with your script.<br>
  167. <br>
  168.  
  169. <li>As always, you need to create an instance of the component in order to use it. Add the following code to your script: <FONT FACE="COURIER"><pre><% Set OBJdbConnection = Server.CreateObject("ADODB.Connection")  </pre></FONT>
  170.  
  171. <li>For the Database Access component, you also need to specify the ODBC <em>data source</em> (the database from which you want data) 
  172. by opening a connection to the database. Add the following code to your script: <FONT FACE="COURIER"><pre>OBJdbConnection.Open "AWTutorial" </pre></FONT>
  173.  
  174. <li>Use the Database Access component <strong>Execute</strong> method to issue a SQL <strong>SELECT</strong> query (<strong>SQLQuery</strong>) to the database and store 
  175. the returned records in a result set (<FONT FACE="COURIER"><code>RSCustomerList</code></FONT>). Add the following code to your script below the 
  176. <FONT FACE="COURIER"><code>OBJdbConnection.Open</code></FONT> statement: <br>
  177. <br>
  178. <blockquote>
  179. <FONT FACE="COURIER"><pre>SQLQuery = "SELECT * FROM Customers" 
  180. Set RScustomerList = OBJdbConnection.Execute(SQLQuery) 
  181. %></pre></FONT>
  182. </blockquote>
  183. <p>You could combine these two lines of script code by passing the literal <strong>SELECT</strong> string directly to the <strong>Execute</strong> method rather than
  184. assigning it to <strong>SQLQuery</strong>. When the query string is long, however, it makes the script easier to read if you assign the string to a 
  185. variable name, such as <strong>SQLQuery</strong>, and then pass the variable name on to the <strong>Execute</strong> method. </p>
  186.  
  187. </ol>
  188.  
  189. <h3><A NAME="Displaythereturnedresultset">Display the returned result set</A></h3>
  190.  
  191. <p>You can think of the result set as a table whose structure is determined by the fields specified in the SQL <strong>SELECT</strong> query. Displaying the 
  192. 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 
  193. is displayed in table rows. 
  194.  
  195. <ol>
  196. <li>In Database.asp, find the words "Tutorial Lesson - Display ADO Data," and add the following VBScript <strong>Do...Loop</strong> statement: <FONT FACE="COURIER"><pre>
  197. <% Do While Not RScustomerList.EOF %>
  198.   <TR>
  199.   <TD BGCOLOR="f7efde" ALIGN=CENTER> 
  200.     <FONT STYLE="ARIAL NARROW" SIZE=1> 
  201.       <%= RSCustomerList("CompanyName")%> 
  202.     </FONT></TD>
  203.   <TD BGCOLOR="f7efde" ALIGN=CENTER>
  204.     <FONT STYLE="ARIAL NARROW" SIZE=1> 
  205.       <%=RScustomerList("ContactLastName") & ", " %> 
  206.       <%=RScustomerList("ContactFirstName") %> 
  207.     </FONT></TD>
  208.   <TD BGCOLOR="f7efde" ALIGN=CENTER>
  209.     <FONT STYLE="ARIAL NARROW" SIZE=1>
  210.     <A HREF="mailto:"> 
  211.       <%=RScustomerList("ContactLastName")%> 
  212.     </A></FONT></TD>
  213.   <TD BGCOLOR="f7efde" ALIGN=CENTER>
  214.     <FONT STYLE="ARIAL NARROW" SIZE=1> 
  215.       <%=RScustomerList("City")%> 
  216.     </FONT></TD>
  217.   <TD BGCOLOR="f7efde" ALIGN=CENTER>
  218.     <FONT STYLE="ARIAL NARROW" SIZE=1> 
  219.       <%=RScustomerList("StateOrProvince")%> 
  220.     </FONT></TD>
  221.   </TR> </pre></FONT>
  222.  
  223. <p>The <strong>Do...Loop</strong> statement repeats a block of statements while a condition is true. The repeated statements can be script commands 
  224. or HTML text and tags. Thus, each time through the loop, you construct a table row (using HTML) and insert returned data (using 
  225. script commands).
  226.  
  227. <li>To complete the loop, use the <strong>MoveNext</strong> method to move the row pointer for the result set down one row. Because this 
  228. statement still falls within the <strong>Do...Loop</strong> statement, it is repeated until the end of the file is reached. Replace the comment with the 
  229. words "Tutorial Lesson - Next Row" with the following code: <FONT FACE="COURIER"><pre><% 
  230. RScustomerList.MoveNext 
  231. Loop 
  232. %></pre></FONT>
  233.  
  234. <li>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.
  235. <br>
  236. <br>
  237.  
  238. <li>Test the file in your browser by typing the following address: <a href="http://localhost/aspsamp/tutorial/lessons/database.asp">http://localhost/aspsamp/tutorial/lessons/database.asp</a></li>
  239. </ol>
  240.  
  241. <h3><A NAME="Onyourown">On your own </A></h3>
  242.  
  243. <p>To see a more complete example of the Database Access component, look at the file <a href="http://localhost/advworks/internal/Customer_listing.asp">Customer_Listing.asp</a> in the Adventure Works 
  244. sample site. Click the <Strong>Show Script Code </Strong> button  to see the code that constructs the customer list. 
  245.  
  246. <hr>
  247.  
  248. <p align=center><a href="/iasdocs/aspdocs/sdklegal.htm"><em>© 1996 Microsoft Corporation. All rights reserved.</em></a></p>
  249. </FONT>
  250. </BODY>
  251. </html>
  252.