Free Web Site - Free Web Space and Site Hosting - Web Hosting - Internet Store and Ecommerce Solution Provider - High Speed Internet
Search the Web

Writing ASP Scripts

Web Tips :   ASP (Active Server Page) Tips


This section introduces the basic elements of Active Server Pages (ASP) scripts. For a hands-on introduction to ASP scripting, see the ASP Tutorial.

What is an .Asp File?

Active Server Pages (ASP) is built around files with the file name extension .asp. An .asp file is a text file and can contain any combination of the following:

Its easy to create an .asp file: Just rename any HTML file, replacing the existing .htm or .html file name extension with .asp. To make the .asp script file available to Web users, save the new file in a Web publishing directory (be sure that the associated virtual directory has Execute permissions enabled). When you view the file with your browser, you see that ASP processes and returns HTML, just as before. For more information about Web publishing, virtual directories, and setting permissions, refer to your Microsoft Web servers online documentation.

ASP really begins to work for you, however, when you add scripts to your HTML.

What is a Script?

A script is a series of script commands. A script can, for example:

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 provides scripting engines for the VBScript and JScript scripting languages. Your primary scripting languagethat is, the language that ASP assumes you are using if you don't specify a languageis VBScript by default. For more information on how to use these and other scripting languages with ASP, and on how to change your default primary scripting language, refer to Using Scripting Languages.

ASP Syntax

ASP is not a scripting language; rather, ASP provides an environment that processes scripts that you incorporate into your HTML pages. To use ASP 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. In the case of HTML, these delimiters are the less than (<) and greater than (>) symbols.

Similarly, ASP script commands and output expressions are differentiated from both text and HTML tags by delimiters. ASP uses the delimiters <% and %> to enclose script commands. For example, the command <% sport = "climbing" %> assigns the value climbing to the variable sport.

ASP uses the delimiters <%= and %> to enclose output expressions. For example, the output expression <%= sport %> sends the value climbing (the current value of the variable) to the browser.

Single Expressions

You can include within ASP delimiters any expression valid for your primary scripting language. For example, the following line produces text 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 function Now to the browser along with the text.

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 VBScript 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 lines send the value, in green, to the client browser:

<FONT COLOR="GREEN"> 
<%= greeting %> 
</FONT>

Thus, a user viewing this script before 12:00 noon (in the Web servers time zone) would see

Good Morning!

A user viewing the script at or 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:

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

If the condition is truethat is, if the time is midnight or after, and before noonthen the Web Server sends the HTML that follows the condition (Good Morning) to the browser; otherwise, it sends the HTML that follows Else (Hello) to the browser.

Script Tags

The statements, expressions, commands, and procedures 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, with ASP you can use other scripting languages; just use the HTML script tags <SCRIPT> and </SCRIPT>, together with the LANGUAGE and RUNAT attributes, to enclose complete procedures written in any language for which you have the scripting engine.

For example, the following .asp file processes the JScript procedure MyFunction.

<HTML>
<BODY>
<% Call MyFunction %>
</BODY>
</HTML>

<SCRIPT RUNAT=SERVER LANGUAGE=JSCRIPT>
  function  MyFunction ()
  {
      Response.Write("MyFunction Called")
  }  
</SCRIPT>

Important    Do not include within <SCRIPT> tags any output expressions or script commands that are not part of complete procedures.


You can include procedures written in your default primary scripting language within ASP delimiters. Refer to Using Scripting Languages for more information.

Including Other Files

Server-side includes is a mechanism you can use to insert information into a file prior to processing. ASP implements only the #INCLUDE pre-processing directive of this mechanism. You can use this directive to insert the content of another file into an .asp file before ASP processes the .asp file. Use the following syntax:

<!--#INCLUDE VIRTUAL|FILE="filename"--> 

Where you must type either VIRTUAL or FILE, which are keywords that indicate the type of path you are using to include the file, and filename is the path and file name of the file you want to include.

Included files do not require a special file-name extension; however, Microsoft recommends giving included files an .inc file-name extension to distinguish them from other types of files.

Using the Virtual Keyword

Use the Virtual keyword to indicate a path beginning with a virtual directory. (For information about using virtual directories, refer to your Microsoft Web servers online documentation.) For example, if a file named Footer.inc resides in a virtual directory named /Myapp, the following line would insert the contents of Footer.inc into the file containing the line:

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

Using the File Keyword

Use the File keyword to indicate a relative path. A relative path begins with the directory that contains the including file. For example, if you have a file in the directory Myapp, and the file Header1.inc is in Myapp\Headers, the following line would insert Header1.inc in your file:

<!--#INCLUDE FILE="headers/header1.inc"-->

Note that the path to the included file, Headers/header1.inc, 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 also use the FILE parameter with ../ syntax to include a file from a parent, or higher-level, directory if the EnableParentPaths registry setting is 1. Refer to Configuring Registry Entries for more information.

Including Files: Tips and Cautions

An included file can, in turn, include other files. An .asp file can also include the same file more than once, provided that the <INCLUDE> statements do not cause a loop. For example, if the file First.asp includes the file Second.inc, Second.inc must not in turn include First.asp. Nor can a file include itself. ASP detects such loop or nesting errors, generates an error message, and stops processing the requested .asp file.

ASP includes files before executing script commands. Therefore, you cannot use a script command to build the name of an included file. For example, the following script would not open the file Header1.inc because ASP attempts to execute the #Include directive before it assigns a file name to the variable name.

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

Scripts commands and procedures must be entirely 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 an including .asp file, then close the delimiter in an included file; the script or script command must be a complete unit. For example, the following script would not work:

<!-- This script will fail -->
<%
For i = 1 To n
  statements in main file
  <!--#include file="header1.inc" -->
Next
%> 

The following script would work:

<% 
For i = 1 to n
  statements in main file
%> 
<!--#include file="header1.inc" -->
<% Next %> 

Using a Server Script to Modify a Client Script

Although ASP is used primarily to process server-side scripting, you can extend its reach by using it to generate client-side scripts that are then processed by the client 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 in your scripts, you can create exciting applications. For example, the following script uses a database to provide a particular client script as a result of the users actions.

In the following 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 links in the page displayed in the client browser.

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

<!-- This script is incomplete -->
<%
Set rsAlbums = Server.CreateObject("ADODB.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
%>

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


© 1996 Microsoft Corporation. All rights reserved.