Module 1: Creating ASP Pages

In this module, you will learn some ActiveX basics by creating your own ASP pages (.asp files). You will find the example files you are to use in these lessons in the Tutorial\Lessons directory in your Active Server Pages samples directory (\InetPub\ASPSamp\Tutorial by default). Save the files you create in the Tutorial\Lessons directory as well.


Lesson 1: Creating a Simple ASP Page

The best way to learn about ASP pages is to write your own. To create an ASP page, use a text editor to insert script commands into an HTML page. Saving the page with an .asp file extension tells ASP to process the script commands. To view the results of a script, simply open the page in a Web browser. In this lesson, you will create the popular “Hello World!” script by copying HTML and ASP scripting commands from this tutorial page into a text editor. You can then view the script's output with your browser.

Creating ASP pages

The following HTML commands create a simple page with the words “Hello World!” in a large font:

<HTML> 
<FONT SIZE=7> 
Hello World!<BR> 
</FONT> 
</HTML> 

Suppose you wanted to repeat this text several times, increasing the font size with each repetition. You could repeat the font tags and HTML text, giving it a different font size with each repetition. When a browser opens the HTML page, the line will be displayed several times.

Alternatively, you could use ASP to generate this content in a more efficient manner.

Create and save a page

  1. Start a text editor (such as Notepad) or a word processor (such as Microsoft® Word). Position the text editor window and the browser window so that you can see both.
  2. Type the following HTML tag at the beginning of the file: <HTML>
  3. Save the document as Hello.asp in the Lessons directory (\InetPub\ASPSamp\Tutorial\Lessons by default). Be sure to save the file in text format if you are using a word processor, including WordPad. ASP pages must have the .asp extension so that ASP knows to process the page.
  4. Start a new line after the <HTML> tag and type the following code: <% For i = 3 To 7 %>

    Script commands are enclosed within <% and %> characters (also called delimiters). Text within the delimiters is processed as a script command. Any text following the closing delimiter is simply displayed as HTML text in the browser. This script command begins a VBScript loop that controls the number of times the words "Hello World" are displayed. The first time through the loop, the counter (i) is set to 3. The second time the loop is repeated, the counter is set to 4. The loop is repeated until the counter exceeds 7.

  5. Press ENTER, then type the following line: <FONT SIZE=<% = i %>>

    Each time through the loop, the font size is set to the current value of the counter (i). Thus, the first time the text is displayed, the font size is 3. The second time, the font size is 4. The last time, the font size is 7. Note that a script command can be enclosed within an HTML tag.

  6. Press ENTER, then type the following lines:
    Hello World!<BR> 
    <% Next %>
    </HTML>

    The VBScript Next expression repeats the loop (until the counter exceeds 7).

  7. Save your changes. Be sure to save your file in text format and be sure the file name extension is .asp.

    Some text editors automatically change the file extension to .txt when you choose Text Format in the Save dialog box. If this happens, replace the .txt extension with the .asp extension before you click Save.

  8. Exit your text editor. A browser might not be able to read an HTML page that is open in a text editor.
  9. To view the results of your work (after which you can return to this Tutorial by clicking the Back button in your browser), point your browser to http://localhost/aspsamp/tutorial/lessons/hello.asp.

    You should see a Web page with “Hello World!” displayed five times, each time in a larger font size.

Congratulations! You have completed your first ASP page. As you have learned, the process of creating an ASP page is simple. You can use any text editor to create HTML content and ASP commands (enclosed in <% and ,%> delimiters) as long as you give your files an .asp file extension. To test a page and see the results, you open the page in a Web browser (or refresh a previously opened page).


Lesson 2: Creating an HTML Form

A common use of Internet and intranet server applications is to process a form submitted by a browser. Previously, you needed to write a program to process the data submitted by the form. With ASP, you can embed scripts written in VBScript or JScript directly into an HTML file to process the form. ASP reads the scripts, performs the commands, and returns the results to the Web browser.

In this lesson, you will create an ASP page that processes the data a user submits by way of an HTML form.

To see how the server page works, fill in the form below. You can use the TAB key to move around the form. Click the Submit button to send your data to the server.



Create the Form

We have created a form to request user information; you can find it in the file Form.htm in the Lessons directory:

<HTML>
<HEAD><TITLE>Order</TITLE></HEAD>

<BODY>
<H2>Sample Order Form</H2>
<P>
Please provide the following information, then click Submit:

<FORM METHOD="POST" ACTION="response.asp">
<P>
First Name: <INPUT NAME="fname" SIZE="48">
<P>
Last Name: <INPUT NAME="lname" SIZE="48">
<P>
Title: <INPUT NAME="title" TYPE=RADIO VALUE="mr">Mr.
<INPUT NAME="title" TYPE=RADIO VALUE="ms">Ms.

<P><INPUT TYPE=SUBMIT><INPUT TYPE=RESET>
</FORM>

</BODY>

</HTML>

Like all forms, this one sends the data to the Web server as pairs of variables and values. For example, the name the user types in the First Name text box is assigned to the variable named fname. ASP provides built-in objects that you can use to access the variable and value pairs submitted by a form.

Create the ASP Response Page

  1. Use your text editor to open the Response.asp file in the Lessons directory (\InetPub\ASPSamp\Tutorial\Lessons by default).

    This file contains the HTML page that the Web server returns to the client browser. You will add ASP script commands to this page to process the information from the form.

  2. Search for the words "Tutorial Lesson" and replace the entire line with the following code:
    <% Title = Request.Form("title") 
    

    Your form transmits three types of information to ASP:

    • fname
    • lname
    • title
    ASP stores information submitted by way of HTML forms in the Forms collection of the Request object. (You can learn more about objects and forms in Active Server Pages Scripting Guide and Active Server Pages Object Reference). To retrieve information from the Request object, you type the following: Request.collection-name ("property-name"). Thus, Request.Form ("title") retrieves mr or ms, depending on the value the user submitted.
  3. Insert the following lines of script following the line you inserted in Step 2:
    LastName = Request.Form("lname")
    If Title = "mr" Then %>  
    Mr. <% = LastName %>  
    <% ElseIf Title = "ms" Then %> Ms. <% = LastName %> 
    

    The VBScript If...Then statement performs two different actions depending on the value of Title. If Title is mr, the user will be addressed as " Mr." If Title is ms, the user will be addressed as "Ms." You display the value of a variable by using the expression <%=variable-name%>:

  4. To display both the first and last name if the user did not choose a title, insert the following lines of script following the line you inserted in Step 3:
    <% Else %>
    <% = Request.Form("fname") & " " & LastName %>
    <% End If %> 
    

    The ampersand (& ) joins the values of the variables into one string. The End If statement ends the conditional expression.

  5. Save Response.asp and exit the text editor. Be sure your text editor does not replace the .asp extension.
  6. To verify that the form you've created works (after which you can return to this Tutorial by clicking Back in your browser), point your browser to http://localhost/aspsamp/tutorial/lessons/form.htm

Congratulations! You have activated your first HTML form. The world of Active server components awaits you in Module 2: Using Active server components.


© 1996 Microsoft Corporation. All rights reserved.