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.
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.
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.
<HTML>
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. <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.
<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.
Hello World!<BR> <% Next %> </HTML>
The VBScript Next expression repeats the loop (until the counter exceeds 7).
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.
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).
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.
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.
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.
<% Title = Request.Form("title")
Your form transmits three types of information to ASP:
Request.collection-name ("property-name")
.
Thus, Request.Form ("title") retrieves mr
or ms
, depending on the value the user submitted.
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%>
:
<% 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.
Congratulations! You have activated your first HTML form. The world of Active server components awaits you in Module 2: Using Active server components.