Microsoft KB Archive/299983

= How to use Windows script components to package ASP scripts =

Article ID: 299983

Article Last Modified on 3/23/2007

-

APPLIES TO


 * Microsoft Active Server Pages 4.0

-



This article was previously published under Q299983





Notice
We strongly recommend that all users upgrade to Microsoft Internet Information Services (IIS) version 6.0 running on Microsoft Windows Server 2003. IIS 6.0 significantly increases Web infrastructure security. For more information about IIS security-related topics, visit the following Microsoft Web site:

http://www.microsoft.com/technet/security/prodtech/IIS.mspx



SUMMARY
This article demonstrates how to use Windows script components (WSC) to package scripts for use in Active Server Pages (ASP) applications. This component includes an entry in the registry and a type library. You can call this component just as you call a compiled component. Also, because the component is a text file, you can modify your code repeatedly during development without having to recompile.

Requirements
 The steps in this article require one of the following configurations:  Microsoft Windows 2000 Professional, Windows 2000 Server, or Windows 2000 Advanced Server with Internet Information Server (IIS) 5.0 Windows NT 4.0 and IIS 4.0 with Windows Scripting Host 5.5 and Microsoft Internet Explorer 5 or later.  A valid, configured connection to Microsoft SQL Server 7.0 or 2000, which uses an OLE DB Provider connection string to the sample Pubs database.

Note In the &quot;Data Source=&quot; parameter of the first code sample to follow, you must replace &quot;&quot; with the name of your database server.

If you do not have access to SQL Server, the sample should still work if you substitute a valid connection string to the Microsoft Access Northwind sample database, change the SQL string to connect to the Employees table, and use two different fields (one for employee ID and one for last name). For more information about how to set up a database connection, click the following article number to view the article in the Microsoft Knowledge Base:

300382 How to create a database connection from an ASP page in IIS

</ul>

Create the Web root and application folder
<ol> Right-click Start, and then click Explore to open Windows Explorer.</li> Click to highlight the C drive (or main drive).</li> From the File menu, point to New, and then click Folder. Name the folder WSC .</li> Right-click WSC, and then click Properties.</li> On the Security tab, click Everyone. In the Permissions text box, ensure that the Read and Execute check boxes are selected. Leave any other check boxes as they are. Click OK to close the WSC Properties dialog box.</li> Click to highlight the root folder of your Web site (which is C:\InetPub\Wwwroot by default).</li> From the File menu, point to New, and then click Folder. Name the folder WscTest .</li> From the Windows Start menu, point to Programs, point to Administrative Tools, and then click Internet Services Manager to open the ISM.</li> Notice that the WscTest folder appears under the Default Web Site. Right-click WscTest, and then click Properties.</li> Confirm that Anonymous access is enabled as follows: <ol style="list-style-type: lower-alpha;"> On the Directory Security tab, under Anonymous Access and Authentication control, click Edit.</li> Select the Anonymous Access check box, click OK, and then click Apply.</li></ol> </li> Mark the directory as an Active Server Pages (ASP) application as follows: <ol style="list-style-type: lower-alpha;"> On the Directory tab of the WscTest Properties dialog box, click Create under Application Settings.</li> Click Apply to save your changes, and then click OK to close the WscTest Properties dialog box.</li></ol>

For more information about how to create subwebs and configure them for ASP applications, click the following article number to view the article in the Microsoft Knowledge Base:

301392 How to create a virtual folder (subweb) in IIS 4.0 or in IIS 5.0

</li></ol>

Download and install the Windows Script Component Wizard
<ol> To download the Microsoft Windows Script Component Wizard Setup file (Wz10en.exe), visit the following Microsoft Download Center Web site:

http://www.microsoft.com/downloads/details.aspx?FamilyId=408024ED-FAAD-4835-8E68-773CCC951A6B

</li> <li>After you have downloaded the file, double-click Wz10en.exe to install the wizard.</li> <li>When you are prompted for an installation location, click OK to install the wizard to the default location.</li> <li>When you are prompted to create the installation directory, click OK.</li></ol>

Create a Windows script component

 * 1) From the Windows Start menu, point to Programs, point to Microsoft Windows Script, and then click Windows Script Component Wizard to start the Windows Script Component Wizard.
 * 2) In Step 1 of the wizard, in the Name text box, type HTMLComponent . Notice that the Filename and Prog ID text boxes are populated automatically. In the Location text box, type the path to the WSC folder that you created earlier (for example, type C:\WSC ). Click Next.
 * 3) In Step 2 of the wizard, click JScript as the language. Select the Do you want special implements support? check box, and then click Support Active Server Pages. Select both the Error checking and Debugging check boxes, and then click Next.
 * 4) In Step 3 of the wizard, you are prompted to define the properties of your WSC. For this demonstration, skip this step, and click Next.
 * 5) In Step 4 of the wizard, you are prompted to define the methods of your WSC. In the Name text box, type MakeDropDown . In the corresponding Parameters text box, type strSQL . In the Name column, click below your first entry, and type DateString . This method has no parameter. Click Next.
 * 6) In Step 5 of the wizard, you are prompted to define custom events. For purposes of this sample, skip this step, and click Next.
 * 7) In Step 6 of the wizard, review the selections that you made, and then click Finish.

Modify the script component
<ol> <li>Right-click Start, and then click Explore to open Windows Explorer.</li> <li>Open the WSC folder that you specified in Step 1 of the wizard.</li> <li>Right-click HTMLComponent.wsc, and then click Open to open the WSC in Notepad.</li> <li>Examine the contents of the file and note the following items: <ul> <li>The file is a normal Extensible Markup Language (XML) text file.</li> <li>The &quot;registration&quot; element defines the registry details of your component.</li> <li>The &quot;public&quot; element provides public interfaces for the methods that you defined.</li> <li>The code includes your two functions, each of which include nothing more than a &quot;return&quot; line of code.</li></ul> </li> <li>To dynamically populate a drop-down list box, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>Locate the MakeDropDown function.</li> <li> Delete the following line of code from the MakeDropDown function: return &quot;Temporary Value&quot;; </li> <li> Copy the following code, and paste the code between the &quot;{&quot; and &quot;}&quot; brackets in the function.

Note Remember that you must modify the connection string to work with your database so that the sample code works: //ADO constants var adOpenForWardOnly = 0 var adLockReadOnly = 1 //Open the recordset. var conn = Server.CreateObject(&quot;ADODB.Connection&quot;); conn.Open(&quot;Provider=SQLOLEDB.1;User ID= ;Password= ;&quot;                  + &quot;Initial Catalog=pubs;Data Source=<Your_Database>;&quot;); var rs = Server.CreateObject(&quot;ADODB.Recordset&quot;); rs.Open(strSQL,conn); //Create the HTML string for the drop-down list box. var strHTML = &quot;<SELECT id='myDropDown' name='myDropDown'>\n&quot;; while(rs.EOF != true) { strHTML += &quot;<OPTION value='&quot; + rs.Fields(&quot;au_id&quot;).Value + &quot;'>&quot; + rs.Fields(&quot;au_lname&quot;).Value + &quot;</OPTION>\n&quot;; rs.MoveNext; }       strHTML += &quot;</SELECT>\n&quot;; //Clean up the objects. conn.Close; conn = null; rs = null; //Return the HTML string for the drop-down list box. return strHTML; </li></ol> </li> <li>Modify the DateString function so that it allows for varied date formats and displays them as expected: <ol style="list-style-type: lower-alpha;"> <li>Locate the DateString function.</li> <li> Delete the following line of code from the DateString function: return &quot;Temporary Value&quot;; </li> <li> Copy the following code, and paste the code between the &quot;{&quot; and &quot;}&quot; brackets in the function: /*           This function does not rely on system settings for date formats, which assures that it will display as expected in the browser. */        var s = &quot;Today's Date is:  &quot;; var d = new Date; s += (d.getMonth + 1) + &quot;/&quot;; s += d.getDate + &quot;/&quot;; s += d.getFullYear; return s;                       </li> <li>Save and close the WSC file.</li></ol> </li></ol>

Register the component

 * 1) Right-click Start, and then click Explore to open Windows Explorer.
 * 2) Open the WSC folder, right-click HTMLComponent.wsc, and then click Register.
 * 3) After you register the WSC, right-click HTMLComponent.wsc again, and then click Generate Type Library. This creates the Scriptlet.tlb file in the WSC folder. Scriptlet.tlb enables you to use IntelliSense coding if you are using a development environment such as Microsoft Visual InterDev for your ASP pages.

How to use your component's methods
<ol> <li>From the Windows Start menu, click Run, type notepad, and then click OK to open Notepad.</li> <li> Create a new ASP page that calls HTMLComponent.wsc and uses its MakeDropDown and DateString functions in your page. Copy and paste the following code into the new ASP page: <%@ Language=JScript %> <HTML> <HEAD> <META NAME=&quot;GENERATOR&quot; Content=&quot;Microsoft Visual Studio 6.0&quot;> </HEAD> <BODY> Hello!<BR> <%       var HTMLObj = Server.CreateObject(&quot;HTMLComponent.WSC&quot;) Response.Write(HTMLObj.DateString); %>   <FORM action=&quot;&quot; method=POST id=form1 name=form1> Select Author:<BR> <%       var SQL = &quot;Select au_id, au_lname FROM Authors&quot;; Response.Write(HTMLObj.MakeDropDown(SQL)); %>   </FORM> </BODY> </HTML> Notice in the preceding code that you use Server.CreateObject and pass it the Prog ID (&quot;HTMLComponent.WSC&quot;) of your component to declare an object. Then, all that you have to do is call the two methods, DateString and MakeDropDown, and use Response.Write to write the returned values into the page. </li> <li>From the File menu, click Save. In the Save In dialog box, browse to the WscTest folder that you created earlier. In the File name text box, type Test.asp, and then click Save.</li></ol>

View your page
After you view the page in the browser, return to the Web server. In Notepad, open and compare the Test.asp and the HTMLComponent.wsc files. Notice how much cleaner the ASP page looks, with only two lines of code to call methods in the HTMLComponent that combined take up over 40 lines. It is much easier to read and maintain your ASP application if you use WSC components to package reusable code.

Troubleshooting

 * Windows script components are not compiled; thus, they will not match the performance of compiled components.
 * Because Windows script components are XML files and real COM components, if you manually modify the component file (except to change the code in your defined methods), you can easily cause your component to fail or generate unexpected errors.
 * WSC components are a newer Microsoft technology. Despite their strengths, they are not widely documented or used yet. Unlike traditional Visual Basic COM components or regular ASP pages, a quantity of books and articles does not exist yet to provide help.