Export (0) Print
Expand All

Active Server Pages Tutorial

 

Microsoft Corporation
Updated December 27, 2000

Summary: This two-module tutorial provides a step-by-step introduction to several ASP features. The tutorial shows how to build sample applications and demonstrates key concepts you'll need to understand when developing your own ASP pages. (11 printed pages)

Welcome to the Active Server Pages (ASP) tutorial. If you are new to Microsoft® Internet Information Services (IIS), this introduction provides necessary information about tutorial directory structure and permission issues. The two tutorial modules show you how to build sample applications, while demonstrating key concepts you'll need to understand when developing your own ASP pages. We suggest you work through each lesson in Module One before starting Module Two.

This article contains the following sections:

  • Introduction. This section defines what ASP is, how it works, and other technologies that can be used in conjunction with ASP to create active content on your Web site. It also describes the directory structure you should use for the tutorial and security considerations that may affect your ability to run the tutorial.
  • Module One. This module introduces the creation of an ASP page, and focuses on the collection and transfer of data. It contains the following lessons:
  • Module Two. This module contains the following lessons:

Introduction

Microsoft® Active Server Pages (ASP) is a server-side scripting technology that can be used to create dynamic and interactive Web applications. An ASP page is an HTML page that contains server-side scripts that are processed by the Web server before being sent to the user's browser. You can combine ASP with Extensible Markup Language (XML), Component Object Model (COM), and Hypertext Markup Language (HTML) to create powerful interactive Web sites.

Server-side scripts run when a browser requests an .asp file from the Web server. ASP is called by the Web server, which processes the requested file from top to bottom and executes any script commands. It then formats a standard Web page and sends it to the browser.

It is possible to extend your ASP scripts using COM components and XML. COM extends your scripting capabilities by providing a compact, reusable, and secure means of gaining access to information. You can call components from any script or programming language that supports Automation. XML is a meta-markup language that provides a format to describe structured data by using a set of tags.

As you work through the lessons in each module, save your work in the C:\Inetpub\Wwwroot\Tutorial directory to view the content on your Web site. If you did not customize your installation of IIS, C:\Inetpub\Wwwroot was created as the default home directory. To load and run the tutorial samples, you must have administrator privileges on the computer running IIS. The default IIS security settings should allow you to run the tutorial, but you may need to change your security settings on the directory if you encounter access violations. See the product documentation for more information about IIS security settings.

Module One

This module explains how to create ASP pages (.asp files) that use the fundamental features of HTML, ASP, and VBScript. This module includes the following lessons:

Write and Run an ASP Page

The best way to learn about ASP pages is to write your own. This lesson covers VBScript syntax and coding samples. To create an ASP page, use a text editor to insert script commands into an HTML page. Saving the page with an .asp file name extension tells the Web server how to process the script commands. To view the results of a script, request the page using a Web browser. VBScript is the default scripting language for ASP, and most of the examples in the tutorial are scripted in VBScript.

In HTML, you use brackets as delimiters around the tags:

<example>

In VBScript, you use the percent sign with brackets as delimiters around the tags:

<%example%>

You can put many tags inside one pair of VBScript delimiters:

<%example, samples%>

Example 1

This example displays the words "Hello World". To run this example, cut and paste it into an empty file and save it in the C:\Inetpub\Wwwroot\Tutorial directory as Example1.asp. Make sure to save your file with an .asp extension. Some text editors automatically change the file name extension to .txt when the Text Document option is selected in the Save dialog box. To prevent this, select the All Files(*.*) option. Exit your text editor as the server may not be able to display an HTML page that is open in a text editor. View your page with your browser by typing http://<Your Server Name>/Tutorial/Example1.asp in the address bar.

When running Example 1, the page is processed by the Web server in the following sequence:

  1. Assigns the text "Hello World" to the variable FirstVar.
  2. Uses HTML to make an HTML page.
  3. Uses <%FirstVar%> to print out the value of the variable FirstVar.
  4. Ends the HTML page.
<%@ Language=VBScript %>
<html>
<head>
<title>Example 1</title>
</head>
<body>
<%
FirstVar = "Hello world!"
%>
<%=FirstVar%>
</body>
</html>

Example 2

This example incorporates a FOR loop in the ASP page. The FOR loop is a statement that prints "Hello World" 10 times. To create Example 2, use the file from Example 1 and add the FOR loop code as shown in the following code sample. Save the file as Example2.asp. View it in your browser.

<%@ Language=VBScript %>
<html>
<head>
<title>Example 2</title>
</head>
<body>
<%
FirstVar = "Hello world!"
%>
<%FOR i=1 TO 10%>
<%=FirstVar%>
<%NEXT%>
</body>
</html>

Example 3

In this example, a time stamp is added to the ASP page. The word time is a function; it is a predefined VBScript function variable containing the current time. There are more than 90 functions in VBScript. Add the code for time shown in the following code sample to Example2.asp and save it as Example3.asp. View it in your browser.

<%@ Language=VBScript %>
<html>
<head>
<title>Example 3</title>
</head>
<body>
<%
FirstVar = "Hello world!"
%>
The time is: <%=time%> <BR>
<%FOR i=1 TO 10%>
<%=FirstVar%>
<%NEXT%>
</body>
</html>

Example 4

This example displays the message "Good Morning Everyone" if the hour is between 4:00 A.M. and 6:00 P.M. It displays the message "Good Night Everyone" if the hour is between 6:00 P.M. and 4:00 A.M. Add the IF THEN statement in the code shown below to Example3.asp and save it as Example4.asp.

<%@ Language=VBScript %>
<html>
<head>
<title>Example 4</title>
</head>
<body>
<%IF Hour(time)>18 OR Hour(time)<4 THEN%>
Good Night Everyone.
<%ELSE%>
Good Morning Everyone.
<%END IF%>
</body>
</html>

Send Information by Using Forms

A common use of intranet and Internet server applications is to process a form submitted by a browser. With ASP, you can embed scripts written in VBScript directly into an HTML file to process the form. ASP processes the script commands and returns the results to the browser. In this lesson, you create an HTML page that displays various elements of an HTML form. Later in this module, you use this knowledge of forms to build a guest book application on your Web site.

This lesson contains the following examples:

  • Button example. Displays selection buttons in the form.
  • Text example. Displays text boxes in the form.

Button example

In this example, there are three input lines that use "buttons," and two default buttons-RESET and SUBMIT. The Post method is used to send data from the client browser to the Web server. Open your text editor, create a new file, and paste in the following code. Save the file as Button.htm and view the page in your browser.

<html>
<head>
<title>Button Form</title>
</head>
<body>
<FORM NAME="Button Example" METHOD="POST" ACTION="tutorial/button.htm">
Computer Programming Experience:
<P>
<INPUT TYPE="button" NAME="choice" VALUE="Less than 1">Less 
   than 1 year.<BR>
<INPUT TYPE="button" NAME="choice" VALUE="1 to 5">1-5 years.<BR>
<INPUT TYPE="button" NAME="choice" VALUE="More than 5">More 
   than 5 years.
</P>
<P><INPUT TYPE="reset" VALUE="Clear Form">
<INPUT TYPE="submit" VALUE="Submit">
</P>
</form>
</body>
</html>

Text example

In this example, you create text fields in a form. Open a new file in your text editor, paste in the following code, and save the file as Text.htm:

<html>
<head>
<title>Text Form</title>
</head>
<body>
<FORM NAME="Text Example" FORM METHOD="POST" ACTION="tutorial/text.htm">
<TABLE>
<TR>
<TD ALIGN="RIGHT" VALIGN="MIDDLE">Name?
<TD ALIGN="LEFT">
<INPUT TYPE="text" NAME="name" VALUE=""
          SIZE="20" MAXLENGTH="150">
<TR>
<TD ALIGN="RIGHT" VALIGN="MIDDLE">Company?
<TD ALIGN="LEFT">
<INPUT TYPE="text" NAME="company" VALUE=""
          SIZE="25" MAXLENGTH="150">
<TR>
<TD ALIGN="RIGHT" VALIGN="MIDDLE">
Email Address?
<TD ALIGN="LEFT">
<INPUT TYPE="text" NAME="email" VALUE=""
          SIZE="25" MAXLENGHT="150">
</TABLE>
<INPUT TYPE="reset">
<INPUT TYPE="Submit" NAME="Submit" VALUE="Submit">
</form>
</body>
</html>

Create a Guest Book

This lesson describes how to develop a guest book application. Guest books allow visitors to your site a chance to give you feedback. Information such as the visitor's name, e-mail address, and comments can be available to you.

Create the guest book database

You must first create an Access database called Guestbook.mdb and save it in the C:\Inetpub\Wwwroot\Tutorial directory. The database must have the fields with the properties described in the following table.

Field Name Data Type and General Properties
ID AutoNumber, Field Size=Long Integer, New Values=Increment, Indexed=Yes(No Duplicates)
TB1 Text, Field Size=255, Required=No, Allow Zero Length=Yes, Indexed=No
TB2 Text, Field Size=255, Required=No, Allow Zero Length=Yes, Indexed=No
TB3 Text, Field Size=255, Required=No, Allow Zero Length=Yes, Indexed=No
TB4 Text, Field Size=255, Required=No, Allow Zero Length=Yes, Indexed=No
MB1 Memo, Required=No, Allow Zero Length=Yes

After you create the database, you need to create a data source name (DSN) connection to the database so your ASP application can interact with it. The DSN must be created on the Web server that is running the ASP application. If you move the ASP application to a different Web server, you have to re-create the DSN on the new Web server. The following procedure describes how to create a DSN on Windows NT and Windows 2000.

  1. In the ODBC Data Source Administrator, select the ODBC icon.
  2. Select File DSN.
  3. Select Add, select Microsoft Access Driver, and click Next.
  4. Type in a descriptive name for your file DSN (Guestbook) and click Next.
  5. Click Finish, click Select, specify the location of the database file, and select OK.
  6. Click OK twice. After you specify the location of the database file, the ODBC Data Source Administrator creates a file DSN for it.

Now that you have created the database and the DSN, paste the following code into a file named Guestbook.asp and place it in the C:\Inetpub\Wwwroot\Tutorial directory.

<% @Language=VBScript %>
<html dir=ltr>
<head>
<TITLE>Guest Book</TITLE>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<%
IF request.form ("Message")="True" THEN
strTB1=request.form("To")
strTB2=request.form("EMailAdd")
strTB3=request.form("CC")
strTB4=request.form("Subject")
strMB1=request.form("Memo")
IF strMB1 = "" THEN
iLenMB1=255
ELSE
iLenMB1 = Len(strMB1)
END IF
'Connects to the Access driver and Access database in the Inetpub
'directory where the database is saved
strProvider = "Driver={Microsoft Access Driver (*.mdb)};
   DBQ=C:\Inetpub\Wwwroot\Tutorial\guestbook.mdb;"
'Creates an instance of an Active Server component
set objConn = server.createobject("ADODB.Connection")
'Opens the connection to the data store
objConn.Open strProvider
'Instantiate Command object and use ActiveConnection property to
'attach connection to Command object
set cm = Server.CreateObject("ADODB.Command")
cm.ActiveConnection = objConn
'Define SQL query
cm.CommandText ="INSERT INTO Guestbook (TB1,TB2,TB3,TB4,MB1)
   VALUES (?,?,?,?,?)"
'Define query parameter configuration information for guestbook fields
set objparam=cm.createparameter(, 200, , 255, strTB1)
cm.parameters.append objparam
set objparam=cm.createparameter(, 200, , 255, strTB2)
cm.parameters.append objparam
set objparam=cm.createparameter(, 200, , 255, strTB3)
cm.parameters.append objparam
set objparam=cm.createparameter(, 200, , 255, strTB4)
cm.parameters.append objparam
set objparam=cm.createparameter(, 201, , iLenMB1, strMB1)
cm.parameters.append objparam
cm.execute
response.write("Thank you!")
ELSE%>
<h1>Guestbook</h1>
<!--Post information to Guestbook form -->
<form name=guestbook.asp  action="guestbook.asp"  method="POST">
<p>To</p>
<p><input type="Text" name="To"></p>
<p>Email Address</p>
<p><input type="Text" name="EmailAdd"></p>
<p> CC</p>
<p><input type="Text" name="CC"></p>
<p>Subject</p>
<p><input type="Text" name="Subject"></p>
<p>Message</p>
<p><textarea name="Memo" rows=6 cols=70></textarea></p>
<input  type="HIDDEN" name="Message" value="True">
<input type="submit" value="Submit information">
</form>
<%End if%>
</body>
</html>

View the database in a browser

Using a database to collect information left by visitors is a safe and simple way to make your Web site more useful. Once information is entered in a database, you can either open the database in the application in which it was originally created, or you can use a Web page containing another script to view and edit the data. The following is a script that allows visitors to view and edit your database. To learn more about how to limit access to resources on your site, see "NTFS Security, Part 1: Implementing NTFS Standard Permissions on Your Web Site," available at http://www.microsoft.com/technet/iis/ntfssec.asp.

<% @Language=VBScript %>
<html dir=ltr>
<head>
<title>View Guest Book</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<%
'This section makes it possible for visitors to sort the data in the
   columns in ascending order.
if request.form("sort")<> "" THEN
StrSort=request.form("sort")
ELSE
StrSort="TB1 ASC"
END IF
strQuery="SELECT * FROM Guestbook ORDER BY " &StrSort
'Database path statement describing the driver to use and the
   path to the desired database.
strProvider = "Driver=Microsoft Access Driver (*.mdb); DBQ=C:\Inetpub\Wwwroot\Tutorial\guestbook.mdb;"
IF Request("ID") <> "" THEN
strIDNum=Request("ID")
'Creates an instance of an Active server component
set objConn = server.createobject("ADODB.Connection")
'Opens the connection to the data store
objConn.Open strProvider
'Instantiate Command object and use ActiveConnection property to
'attach connection to Command object
set cm = Server.CreateObject("ADODB.Command")
cm.ActiveConnection = objConn
'Define SQL query
cm.CommandText = "DELETE FROM Guestbook WHERE ID = " &strIDNum
cm.Execute
END IF
'Instantiate a Recordset object and open a recordset using
'the Open method
Set rst = Server.CreateObject("ADODB.recordset")
rst.Open strQuery, strProvider
%>
<h1>Guest Book</h1>
<form name=viewdb.asp action=viewdb.asp method=post>
<table border=1 cellspacing=3 cellpadding=3 rules=box>
<%
ON ERROR RESUME NEXT
IF rst.EOF THEN
Response.Write "There are no entries in the database."
ELSE%>
<tr>
<%
'Deletes rows from the database, this cannot be undone
Response.Write "<td width=200><center>Delete Record</center></td>"
FOR i = 1 to rst.Fields.Count -1
Response.Write "<td width=200><input name=sort value=" & rst(i).Name 
   & " type=submit></td>"
NEXT
WHILE NOT rst.EOF %>
<tr>
<%
Response.Write "<td align=left valign=top bgcolor='#ffffff'>
   <a href=viewdb.asp?id=" & rst(0) & ">Delete</a></td>"
FOR i = 1 to rst.fields.count - 1
Response.Write "<td align=left valign=top bgcolor='#ffffff'>" & rst(i)
   &"</td>"
NEXT
rst.MoveNext
WEND
END IF
%>
</table>
</form>
</body>
</html>

Display an Excel Spreadsheet in ASP

This lesson demonstrates a technique that can be used to display a Microsoft Excel spreadsheet in a Web page. There are several standard server components supplied with IIS. One of these is the ActiveX® Data Objects (ADO) component, which is used as a connection mechanism to provide access to data. In the database lesson earlier, we used ADO. We use it again here to show you how to view and edit a spreadsheet with a Web browser.

To prepare your Excel spreadsheet for display in an Active Server Page:

  1. Create a spreadsheet using either Excel 98 or Excel 2000 and save it as ASPTOC.xls in the C:\Inetpub\Wwwroot\ directory.
    Note: Do not include any special formatting or column labels when creating the spreadsheet.
  2. Highlight the rows and columns on the spreadsheet that you want displayed in the Web page.
  3. On the Insert menu, choose Name, and select Define.
  4. If there are any names listed, select them and select Delete.
  5. Type a name for the workbook, select Add, and select OK.

To display the spreadsheet in a Web page, you must create a DSN for the spreadsheet. The process is the same as it was for the database lesson earlier in this module. The only difference is that you must select the {Microsoft Excel Driver (*.xls)} option for the DSN.

Once you have created the spreadsheet and named the table in the spreadsheet, and also created a DSN for the spreadsheet, you are ready to create the page that displays the contents. Paste the following code into a new file and name it ASPTOC.asp. Save it in the C:\Inetpub\Wwwroot\Tutorial directory and view the page in your browser.

<%@Language=VBScript %>
<html>
<head>
<title> Displaying An Excel Spreadsheet in an Web Page </title>
</head>
<body bgcolor="#FFFFFF" text="#000000" >
<h1>ASP Table of Contents</h1>
<%
'Creates an instance of an Active Server Component
 Set oConn = Server.CreateObject("ADODB.Connection")
'Connects to the Excel driver and the Excel spreadsheet
'in the directory where the spreadsheet was saved
strConn = "Driver={Microsoft Excel Driver (*.xls)}; DBQ=C:\Inetpub\Wwwroot\Tutorial\ASPTOC.xls;"
'Opens the connection to the data store
 oConn.Open strConn
'Selects the records from the Excel spreadsheet
strCmd = "SELECT * from `ASPTOC`"
Set oRS = Server.CreateObject("ADODB.Recordset")
'Opens the recordset
oRS.Open strCmd, oConn
'Prints the cells and rows in the table
Response.Write "<table border=1><tr><td>"
'Gets records in spreadsheet as a string and prints them in the table
Response.Write oRS.GetString (, , "</tr><td>", "</td></tr><tr><td>",
   NBSPACE)
%>
</body>
</html>

Module Two

This module explains how to develop an ASP page that delivers services useful in e-commerce. This module includes the following lessons:

Rotate Ad Information. Randomly rotate ads on your Web page.

Redirect Users from Ad Links. Redirect browsers to advertisers' sites when users click on an ad image.

Count Page Hits. Track the number of times users request a page.

Rotate Ad Information

Advertising is big business on the Web. This lesson explains how to take advantage of the Ad Rotator component installed with ASP by describing how to use this component to rotate advertisements on your Web pages. The Ad Rotator component selects an advertisement for your Web page each time the user refreshes or loads the Web page. Two files are required to set up the Ad Rotator component: an Ad Rotator Include file and an ad images data file. By setting up these two files, this component can be called by any ASP page on your site. Changes to the ad parameters are not done on all the sites containing the ad, but to the ad images data file. This saves lots of time if the ad appears on numerous pages within your Web site.

This lesson explains how to:

Write an Ad Rotator Include File. Creates the ad-image links on any page that calls this file.

Create an Ad Image Data File. Specifies global ad-display data and information specific to each ad.

Test the Ad Rotator. Uses an ASP page that calls the Ad Rotator logic Include file and the image data file to display and rotate ads.

Write an Ad Rotator Include file

Include files are used to store information that will be used by more than one ASP or HTML file. By creating an Ad Rotator Include file, when changes need to be made to specific information, you only need to change the information in one location. This lesson will guide you in creating an Ad Rotator Include file containing a function named getAd(). This function randomly selects ads to display on your ASP pages. You can call this file from within any ASP page intended to display the rotated ads. When you test calling the Include file from an ASP page (see Test the Ad Rotator), you will use some images from Microsoft.com for ad images.

Open a new file in your text editor, paste in the following script, and save the file as Adrotatorlogic.inc:

<%
Function getAd()
Dim load

'Create an instance of the AdRotator component
Set load=Server.CreateObject("MSWC.AdRotator")

'Set the target frame, if any. This is the frame
'where the URL will open up. If the HTML page does
'not find the target name, the URL will be opened
'in a new window.

load.TargetFrame = "Target = new"

'Get a random advertisement from the text file.

getAd = load.GetAdvertisement("adimagedata.txt")
End Function
%>

Create an ad images data file

An ad images data file is created to provide information about the ads to be displayed. By placing the data in one text file, when changes need to be made, you only need to change the data in one location. The ASP page (with the logic in the Include file) sends data in the ad images data file to the Ad Rotator component. The component then selects an ad for display.

The data file is divided into two sections that are separated by an asterisk (*). The first section provides information common to all the ads to be displayed. The second section lists data relevant to each ad.

  • The following outlines the structure of an ad images data file:
    • REDIRECTION. URL, the path and name of the ASP file that redirects browsers that select ad images.
    • WIDTH. The width of ad images in pixels. Default is 440.
    • HEIGHT. The height of ad images in pixels. Default is 60.
    • BORDER. The border thickness around ad images. Default is 1.
    • *. Separates the first section from the second section.
    • AdURL. Virtual path and filename of the image file containing the advertisement.
    • AdHomeURL. URL to jump to when this link is selected. To indicate there is no link, use a hyphen.
    • Text. Text to display if browser does not support graphics.
    • Impressions. An integer indicating the relative weight, or probability, that this ad will be selected for display. For example, if two ads were displayed, one with an impression of 3 and the other with 7, then the one with 3 would have a 30 percent probability of being selected, while the other would have a 70 percent probability.

Open a new file in your text editor, paste in the following script, and save the file as Adimagedata.txt:

REDIRECT adrotatorredirect.asp
WIDTH 250
HEIGHT 60
BORDER 0
*    ' separates the general data from the image information
images/windows_logo.gif
http://www.microsoft.com/windows
Microsoft Windows
2
images/office_logo.gif
http://www.microsoft.com/office
Office 2000
3

Test the Ad Rotator

To test the system, you will need an ASP page that calls the Ad Rotator Include file and the ad images data file, and then displays the ads. First, you will need test ad images stored on your site.

Acquire and store the ad images as follows:

  1. Create a directory named Images in the tutorial folder: C:\Inetpub\Wwwroot\Tutorial\Images.
  2. Download the Office logo file, available at http://www.microsoft.com/office/images/office_logo.gif, from the Microsoft Office Web site, and save it as Office_logo.gif in C:\Inetpub\Wwwroot\Tutorial\Images.
  3. Download the Windows logo file, available at http://www.microsoft.com/windows/images/bnrwinfam.gif(, from the Microsoft Windows Web site, and save it as Windows_logo.gif in C:\Inetpub\Wwwroot\Tutorial\Images.

Open a new file in your text editor, paste in the following script, and save the file as Displayad.asp:

<%@ Language="VBScript" %>
<!--Call the Include file, which in turn -->
<!--calls the ad images data text file-->
<!--#include file="adrotatorlogic.inc"-->
<html>
<head>
<title>Test Ad Display Page</title>
</head>
<body>
<h1>Test Ad Display Page</h1>
<!--Display the ad banner-->
<p><%=getAd()%></p>
<p>The space above should contain an image displaying either the
   Microsoft Windows family logo or the Microsoft Office
   family logo.</p>
<ul>
<li type="disc">This indicates that Displayad.asp, Adrotatorlogic.inc,
   and Adimagedata.txt are working together to correctly display the
   images.</li>
<li type="disc">Repeatedly refreshing the page should result in the
   Windows and Office logos displaying in random order.</li>
<li type="disc">After you save Adrotatorredirect.asp, clicking on the
   displayed image should cause a new window to open, displaying the
   home page corresponding to the displayed logo.</li>
<li type="disc">After you add the hit counter script to this page,
   under "Statistics" you should see the number of page hits since
   the script was added.</li>
</ul>
<br><br><br><br><br>
<h3>Statistics</h3>
<% Set pageCount = Server.CreateObject("MSWC.PageCounter") %>
<!--Increment the counter-->
<% pageCount.PageHit %>
<p>You are visitor number <% =pageCount.Hits %> to this Web page.</p>
</body>
</html>

Redirect Users from Ad Links

When a user clicks the ad, the browser appends a query string to the request to the server. Then, the server directs the user's browser to the ad's URL.

Open a new file in your text editor, paste in the following script, and save the file as Adrotatorredirect.asp:

<%@Language=VBScript %>
<html>
<head>
<title>Redirection Page</title>
</head>
<body>
<%
'Set the response buffer on
Response.Buffer = True
Dim lsURL
'Obtain the URL from the query string
lsURL = Request.QueryString("URL")
'Clear the response and redirect to URL
Response.Clear()
Response.Redirect(lsURL)
%>
</body>
</html>

To check your work, use Displayad.asp. When you click on an ad, you should see a new window displaying an appropriate ad-related Web page.

Count Page Hits

It is important to know how many hits your Web pages get. This data helps determine how changes to your Web site may affect your customers' viewing habits. More importantly, it provides useful insight as to how customers are navigating through your site and where ads should be placed. Sites with high trafficking have higher advertisement prices associated with them. The data gathered from a page hit counter provides you with trafficking information to begin negotiating advertisement prices for your Web pages.

The Page Counter component uses an internal object to record page hit-count totals for all pages on the server. At regular intervals, this object saves all information to a text file so that no counts are lost due to power loss or system failure. The Page Counter component uses the following three methods:

  • Hits(). This displays the number of hits for a Web page. The default is the current page.
  • PageHit(). This increments the hit count for the current page.
  • Reset(). This resets the hit count for a page to zero. The default is the current page.

An in-use sample of this script can be seen at the bottom of the Displayad.asp script. To place a page hit counter on a Web page, place the following script where you want the counter displayed on the page:

<% Set pageCount = Server.CreateObject("MSWC.PageCounter") %>
<% pageCount.PageHit %>
You are visitor number <% =pageCount.Hits %> to this Web site.

Show:
© 2014 Microsoft