Writing Active Server Pages Scripts

This section introduces basic concepts you need to create Active Server Pages (ASP) scripts. If you have never used a scripting language before, Active Server Pages Tutorial offers a hands-on introduction to topics discussed in more detail here.

What is an .Asp File?

Active Server Pages (ASP) is built around files having the file name extension .asp. An .asp file is an ASCII text file that contains any combination of the following:

It’s easy to create an .asp file: Just rename any HTML file, replacing the existing .htm or .html file name extension with .asp. To view the .asp file, save the new file in a directory that is accessible by way of HTTP; that is, a directory that your Web Server serves. (For more information about HTTP publishing, refer to Microsoft Internet Information Server Installation and Administration Guide.) When you view the file with your browser, you see that ASP processes and returns HTML, as usual.

This is the simplest type of .asp file. ASP does not really begin to work for you, however, until you add scripts to your HTML.

What is a Script?

A script is a series of commands. A script can:

Executing a script sends the series of commands to a scripting engine, which interprets and relays them to your computer. Scripts are written in languages that have specific rules; thus, if you want to use a given scripting language, your server must run the scripting engine that understands the language. ASP Scripting provides scripting engines for the VBScript and JScript scripting languages. Your primary scripting language—that is, the language that ASP assumes you are using when you don't specify a language—is VBScript by default. For more information on how to use these and other scripting languages with ASP, and on how to change your default inline scripting language, refer to Using Scripting Languages.

ASP Syntax

ASP is not a scripting language; rather, ASP provides an environment in which you can run scripting languages from your HTML pages. In order to use this environment successfully, you need to learn the syntax, or rules, by which it operates.

Delimiters

HTML tags are differentiated from text by delimiters. A delimiter is a character or sequence of characters that marks the beginning or end of a unit of data. In the case of HTML, these delimiters are the less than (<) and greater than (>) symbols.

Similarly, script commands and output expressions are differentiated from both text and HTML tags by the delimiters <% and %>. For example:

The Equal Sign

Note the different uses of the equal sign (=) in the previous section’s examples. You can use the equal sign:

Single Expressions

You can include within ASP delimiters any expression valid for your primary scripting language. For example, the following line produces a string ending with the current server time:

This page was last refreshed at <% =Now %>. 

In this case, the Web server returns the value of the VBScript expression Now to the browser along with the text.

To send the contents of a variable or expression back to a user, preface it with an equal sign.

<% name = "Bill" %> 
    Hello, <% =name %>. 

The output from the previous lines would be:

Hello, Bill. 

Statements

A statement, in VBScript and other scripting languages, is a syntactically complete unit that expresses one kind of action, declaration, or definition. The conditional If...Then...Else statement that appears below is a common VBSript statement.

<% 
If Time  > = #12:00:00 AM# And Time < #12:00:00 PM#  Then 
  greeting = "Good Morning!" 
Else 
  greeting = "Hello!" 
End If
%> 

This statement stores either the value "Good Morning!" or the value "Hello!" in the variable greeting. It does not send any values to the client browser. The following line sends the value to the client browser:

<CENTER> <H5> <% =greeting %> </H5> </CENTER> 

Thus, a user viewing this script before 12:00 noon in the server’s time zone would see

Good Morning!
A user viewing the script after 12:00 noon would see
Hello!

Including HTML in a Statement

You can include HTML text between the sections of a statement. For example, the following script, which mixes HTML within an If...Then...Else statement, produces the same result as the script in the previous section:

<CENTER> <H5> 
<% If Time  > = #12:00:00 AM# And Time < #12:00:00 PM#  Then %> 
Good Morning!

<% Else %> 
Hello!
<% End If %> 
</H5> </CENTER> 
If the condition is true—that is, if the time is after midnight and before noon—then the Web Server sends the HTML that follows the condition to the browser; otherwise, it sends the HTML that follows Else to the browser.

Script Tags

The commands, statements, and expressions that you use within script delimiters must be valid for your default primary scripting language. (ASP is shipped with the default primary scripting language set to VBScript.) However, you can use the HTML script tags <SCRIPT> and </SCRIPT>, together with the LANGUAGE and RUNAT attributes, to enclose scripts from any language for which you have the scripting engine.

For example,
<SCRIPT LANGUAGE=JScript RUNAT=Server>
sample script
</SCRIPT>

runs any script that you insert between the <SCRIPT> and </SCRIPT> tags.

Refer to Using Scripting Languages for more information.

Including Other Files

You can use Server-Side Includes syntax to insert the content of another file at a particular place in your script. You can use either virtual paths or relative paths to locate a file.

For example, if you have a file named Footer.htm in a virtual directory called /Myapp, the following line would insert the contents of that file into the script being processed:

<!--#INCLUDE VIRTUAL="/myapp/footer.htm"--> 

If your script is in the directory Myapp, and the file Header1.htm is in Myapp\Headers, the following line would insert Header1.htm:

<!--#INCLUDE FILE="headers\header1.htm"-->

Note that the path to the included file, /Headers\header1.htm, is relative to the including file; if the script containing this Include statement is not in the directory /Myapp, the statement would not work.

You can use the FILE parameter with ..\ syntax to include a file from a parent directory if the EnableParentPaths registry setting is 1. Refer to Configuring Registry Settings for more information.

An included file can, in turn, include other files. An .asp file can also include the same file more than once. Make sure the <INCLUDE> statements do not cause a loop. For example, if file First.asp calls Second.asp, make sure that Second.asp does not call First.asp. A file cannot call itself. The ActiveX Server detects such loop or nesting errors, generates an error message, and stops processing files.

Files are included before script commands are executed. Therefore, you cannot build the name of an included file by using a script command. For example, the following script would not open the file Header1.htm because the Include command is executed before the Set command:

<!-- This script will fail -->
<% set name=(header1 & ".htm") %> 
<!--#include file=" <% name %> "-->

Scripts and script commands in included files must be contained within the script delimiters <% and %>, the HTML tags <SCRIPT> and </SCRIPT>, or the HTML tags <OBJECT> and </OBJECT>. That is, you cannot open a script delimiter in the main .asp file and close it in the included file; the script or script command must be a complete unit. For example, the following code would not work:

<!-- This script will fail -->
<%
  for i = 1 to n
  statements in main file
  <!--#include file=c:\asp\code.asp -->
next
%> 

The following code would work:

<% 
for i = 1 to n
  statements in main file
 %> 
<!--#include file=c:\asp\code.asp -->
<% next %> 

Using a Server Script to Modify a Client Script

While ASP is used primarily to process server-side scripting, you can extend its functionality by using it to generate client-side scripts that are then processed by the Web browser. ASP does this by combining client-side scripts that are enclosed by HTML comments with server-side scripts that are enclosed by delimiters:

<SCRIPT LANGUAGE="VBScript">	
<!--
client script
<% server script %> 
client script
<% server script %>
client script
...
-->
</SCRIPT>

With this functionality introduced in your scripts, you can create dynamic applications that make for interactive experiences for the user. To illustrate the potential of this functionality, the following script example provides a scenario in which a database is used to provide content that is modified as a result of the user’s actions.

In the script, ASP retrieves data from the database (in this case, data pertaining to musical artists and albums) and generates a subroutine for each line of data. These subroutines then control what happens when a user clicks on hotlinks in the .asp page.

Note   This script will not function by itself. It is used to illustrate the functionality of ASP if used in conjunction with a database, server-side scripting, and client-side scripting.

<!-- This script will fail -->
<%
    Set rsAlbums = Server.CreateObject("ADOFX.Recordset")
    rsAlbums.Open "SELECT Artists.*, Albums.* FROM Albums INNER JOIN Artists ON
Albums.ArtistID = Artists.ArtistID", Session("Conn"), 1, 2
%>

<% 
do while rsAlbums.EOF = False%>
<SCRIPT LANGUAGE="VBScript">
<!--Sub Enhanced_OnLoad()
	enhanced.DrawBuffer = 500000
End Sub
Sub albumhotspot<%=rsAlbums("AlbumID")%>_MouseEnter()
	AlbumName.Caption = "<%=rsAlbums("AlbumName")%>"
	ArtistName.Caption = "<%=rsAlbums("ArtistName")%>"
	Divider.visible = true
end sub
Sub albumhotspot<%=rsAlbums("AlbumID")%>_Click()
	Window.location.href = "details.asp?Albumid=<%=rsAlbums("AlbumID")%>"
End Sub-->
</SCRIPT>
<%	rsAlbums.MoveNext
Loop%>

This script can be expanded to create controls that are associated with the user events. The end result is a dynamically generated set of controls with dynamically generated event handlers. Writing scripts such as this allows the Web developer to create functions and subroutines that manipulate database information and save time that would otherwise be used writing detailed script procedures.


© 1996 Microsoft Corporation. All rights reserved.