HOW TO: Use Windows Script Components to Package ASP Scripts
The information in this article applies to:
- Microsoft Active Server Pages
IN THIS TASK
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.
back to the top
Requirements
- Microsoft Windows 2000 Professional,
Windows 2000 Server, or Windows 2000 Advanced Server with Internet
Information Server (IIS) 5.0
-or-
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 "Data Source=" parameter of the first code sample to
follow, you must replace "<Your Database>" 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 additional information about how to set up a database connection,
click the article number below to view the article in the Microsoft
Knowledge Base:
Q300382 HOW TO: Create a Database Connection from an ASP Page in IIS
back to the top
Setup and Installation
Create the Web Root and Application Folder
- Right-click Start , and then click Explore to open
Windows Explorer.
- Click to highlight the C drive (or main drive).
- From the File menu, point to New , and then click
Folder . Name the folder WSC .
- Right-click WSC , and then click Properties .
- 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.
- Click to highlight the root folder of your Web site (which is C:\InetPub\Wwwroot
by default).
- From the File menu, point to New , and then click
Folder . Name the folder WscTest .
- From the Windows Start menu, point to Programs , point
to Administrative Tools , and then click Internet Services
Manager to open the ISM.
- Notice that the WscTest folder appears under the Default Web Site.
Right-click WscTest , and then click Properties .
- Confirm that Anonymous access is enabled as follows:
- On the Directory Security tab, under Anonymous Access and
Authentication control , click Edit .
- Select the Anonymous Access check box, click OK , and
then click Apply .
- Mark the directory as an Active Server Pages (ASP) application as
follows:
- On the Directory tab of the WscTest Properties dialog
box, click Create under Application Settings .
- Click Apply to save your changes, and then click OK to
close the WscTest Properties dialog box.
For additional information about how to create subwebs and configure
them for ASP applications, click the article number below to view the
article in the Microsoft Knowledge Base:
Q301392 HOW TO: Create a Virtual Folder (Subweb) in IIS 4.0 or IIS
5.0
back to the top
Download and Install the Windows Script Component Wizard
- Click the following link to open the Microsoft Windows Script
Component Wizard download site:
Windows Script Component Wizard
- Click English Download .
- Select Yes to accept the End User License Agreement.
- In the File Download dialog box, click Save this program to
disk , and then click OK .
- In the Save As dialog box, save the program to the desktop.
- After the download is complete, click Close in the Download
Complete dialog box.
- On the desktop, double-click Wz10en.exe to install the wizard.
- When you are prompted for an installation location, click OK to
install the wizard to the default location.
- When you are prompted to create the installation directory, click
OK .
back to the top
Create a Windows Script Component
- 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.
- 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 .
- 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
.
- 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 .
- 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 .
- In Step 5 of the wizard, you are prompted to define custom events. For
purposes of this sample, skip this step, and click Next .
- In Step 6 of the wizard, review the selections that you made, and then
click Finish .
back to the top
Modify the Script Component
- Right-click Start , and then click Explore to open
Windows Explorer.
- Open the WSC folder that you specified in Step 1 of the wizard.
- Right-click HTMLComponent.wsc , and then click Open to
open the WSC in Notepad.
- Examine the contents of the file and note the following items:
- The file is a normal Extensible Markup
Language (XML) text file.
- The "registration" element defines the
registry details of your component.
- The "public" element provides public
interfaces for the methods that you defined.
- The code includes your two functions,
each of which include nothing more than a "return" line of code.
- To dynamically populate a drop-down list box, follow these steps:
- Locate the MakeDropDown function.
- Delete the following line of code from the MakeDropDown
function:
return "Temporary Value";
- Copy the following code, and paste the code between the "{" and "}"
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("ADODB.Connection");
conn.Open("Provider=SQLOLEDB.1;User ID=sa;Password=;"
+ "Initial Catalog=pubs;Data Source=<Your_Database>;");
var rs = Server.CreateObject("ADODB.Recordset");
rs.Open(strSQL,conn);
//Create the HTML string for the drop-down list box.
var strHTML = "<SELECT id='myDropDown' name='myDropDown'>\n";
while(rs.EOF != true) {
strHTML += "<OPTION value='" + rs.Fields("au_id").Value + "'>"
+ rs.Fields("au_lname").Value + "</OPTION>\n";
rs.MoveNext();
}
strHTML += "</SELECT>\n";
//Clean up the objects.
conn.Close();
conn = null;
rs = null;
//Return the HTML string for the drop-down list box.
return strHTML;
- Modify the DateString function so that it allows for varied
date formats and displays them as expected:
- Locate the DateString function.
- Delete the following line of code from the DateString
function:
return "Temporary Value";
- Copy the following code, and paste the code between the "{" and "}"
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 = "Today's Date is: ";
var d = new Date();
s += (d.getMonth() + 1) + "/";
s += d.getDate() + "/";
s += d.getFullYear();
return s;
- Save and close the WSC file.
back to the top
Register the Component
- Right-click Start , and then click Explore to open
Windows Explorer.
- Open the WSC folder, right-click HTMLComponent.wsc , and then
click Register .
- 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.
back to the top
How to Use Your Component's Methods
- From the Windows Start menu, click Run , type notepad
, and then click OK to open Notepad.
- 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="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>
Hello!<BR>
<%
var HTMLObj = Server.CreateObject("HTMLComponent.WSC")
Response.Write(HTMLObj.DateString());
%>
<FORM action="" method=POST id=form1 name=form1>
Select Author:<BR>
<%
var SQL = "Select au_id, au_lname FROM Authors";
Response.Write(HTMLObj.MakeDropDown(SQL));
%>
</FORM>
</BODY>
</HTML>
Notice in the preceding code that you use Server.CreateObject
and pass it the Prog ID ("HTMLComponent.WSC") 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.
- 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
.
back to the top
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.
back to the top
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.
back to the top
REFERENCES
For more information, see the following Microsoft Web sites:
Windows Script Components Overview
http://msdn.microsoft.com/scripting/scriptlets/default.htm
Windows Script Components: They Get Around
http://msdn.microsoft.com/library/default.asp?URL=/library/en-us/dnclinic/html/scripting091399.asp
Convert ADO Recordsets to XML with our WSC Component
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnasdj00/html/wsc.asp
For a complete WSC guide, see the "Windows Script Components" topic under
"Tools and Scripting | Scripting" in the Platform Software Development Kit
(SDK) documentation. You can download the Platform SDK from the following
Microsoft Web site:
http://www.microsoft.com/msdownload/platformsdk/sdkupdate/
|