<%@ Language=VBScript EnableSessionState=False %> <% Response.Expires = 0 LessonFile = Request.ServerVariables("SCRIPT_NAME") ScriptLanguagePreference = Request.Cookies("ScriptLanguagePreference") If ScriptLanguagePreference = "" Then ScriptLanguagePreference = Request.QueryString("ScriptLanguagePreference") If ScriptLanguagePreference = "" Then Response.Redirect "/iishelp/iis/htm/asp/iiselect.asp?LessonFile=" & Server.URLEncode(LessonFile) End If End If 'Determine the physical path for the current page and then remove the 'name of the file from the path (leaving just the directory). MainPath = Request.ServerVariables("PATH_TRANSLATED") Length = Len(MainPath) i = 0 Do Until (i = 2) Do While (Mid(MainPath, Length, 1) <> "\") Length = Length - 1 Loop i = i + 1 MainPath = left(MainPath, (Length-1)) Loop FilePath = left(MainPath, Length) + "\tutorial" 'Determine the virtual path for the current page and then remove the 'name of the file from the path (leaving just the directory). MainPath = Request.ServerVariables("PATH_INFO") Length = Len(MainPath) i = 0 Do Until (i = 2) Do While (Mid(MainPath, Length, 1) <> "/") Length = Length - 1 Loop i = i + 1 MainPath = left(MainPath, (Length-1)) Loop VirtFilePath = left(MainPath, Length) + "/tutorial" %> Module 1: Creating ASP Pages

Module 1: Creating ASP Pages

Choose a scripting language for this lesson.

In this module, you will learn some ASP basics by creating your own ASP pages (.asp files). You will find the example files you are to use in these lessons in the <%= Request.ServerVariables("SERVER_NAME")%> Web server's Tutorial directory (<%=FilePath%>). Save the files you create in the Tutorial directory as well.

Important    To save and view your work in this module, you must enable Write and Script Web server permissions for the <%= VirtFilePath %> virtual directory on the <%= Request.ServerVariables("SERVER_NAME")%> Web server (with Active Server Pages installed). For more information, see Setting Web Server Permissions.


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 name extension tells ASP to process the script commands. To view the results of a script, simply request the page in a Web browser by using the HTTP protocol, that is http://<%= Request.ServerVariables("SERVER_NAME")%><%= VirtFilePath %>/filename.asp. In this lesson, you will create the popular “Hello World!” script by copying HTML and ASP scripting commands from this tutorial into a text editor. You can then view the script’s output with your browser after you save the file in the text editor.

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

<HTML> 
<BODY>
<FONT SIZE=7> 
Hello World!<BR> 
</FONT> 
</BODY>
</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 same content in a more dynamic 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. Copy and paste the following HTML tags at the beginning of the file:
  3. <%@ LANGUAGE = "<%=ScriptLanguagePreference%>" %>
    <HTML>
    <BODY> 

    Notice that the very first tag is a special ASP tag that sets your default scripting language to <%=ScriptLanguagePreference%>. Always be sure to add this tag as the first line of all your .asp files.

  4. Save the document as Hello.asp in the <%= Request.ServerVariables("SERVER_NAME")%> Web server's Tutorial directory (<%=FilePath%>). 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 to work properly.
  5. If another user has previously created the Hello.asp file and completed this portion of the tutorial, replace the older version of Hello.asp with your newer version.

  6. Start a new line after the <BODY> tag and copy and paste the following script command: <% If ScriptLanguagePreference = "JScript" Then %>
    <% for (i = 3; i <=7;  i++) { %> 
    <%Else%>
    <% For i = 3 To 7 %> 
    <% End If %>

    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 <%=ScriptLanguagePreference%> loop that controls the number of times the phrase "Hello World" is displayed. The first time through the loop, the counter variable (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 <% If ScriptLanguagePreference = "JScript" Then %>equals<%Else%>exceeds<% End If %> 7.

  7. Press Enter, then copy and paste the following line:
  8. <FONT SIZE=<% = i %>> 

    Each time through the loop, the font size is set to the current value of the counter variable (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.

  9. Press Enter, then copy and paste the following lines:
  10. Hello World!<BR>
     </FONT>
    <%  If ScriptLanguagePreference = "JScript" Then
    %><% } %><%Else%><% Next %><% End If %>
    </BODY>
    </HTML>

    <% If ScriptLanguagePreference = "JScript" Then %>The JScript curly braces ({ }) enclose the content repeated in the loop (as long as the counter is less than or equal to 7).<%Else%>The VBScript Next command repeats the loop (until the counter exceeds 7).<% End If %>

  11. The complete file (Hello.asp) should now contain the following text:
  12. 
    <%@ LANGUAGE = "<%=ScriptLanguagePreference%>" %>
    <HTML>
    <BODY>
    <%
      If ScriptLanguagePreference = "JScript" Then
    %><% for (i = 3; i <=7;  i++) { %><%Else%><% For i = 3 To 7 %> <% End If %>
    <FONT SIZE=<% = i %>> 
    Hello World!<BR>
    </FONT>
    <%  If ScriptLanguagePreference = "JScript" Then
    %><% } %><%Else%><% Next %><% End If %>
    </BODY>
    </HTML>
  13. Save your changes. Be sure to save your file in text format and be sure the file name extension is .asp.
  14. Some text editors automatically change the file name 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.

  15. Exit your text editor. A browser might not be able to request an HTML page that is open in a text editor.
  16. 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 /iishelp/iis/htm/tutorial/hello.asp"> http://<%= Request.ServerVariables("SERVER_NAME")%>/iishelp/iis/htm/tutorial/hello.asp.
  17. 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 script commands (enclosed in

<%
and
%> 
delimiters) as long as you give your files an .asp file name extension. To test a page and see the results, you request the page in a Web browser (or refresh a previously opened page).


Lesson 2: Creating an HTML Form

A common use of intranet and Internet 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 <%=ScriptLanguagePreference%> 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 will create an ASP page that processes the data a user submits by way of an HTML form.

To see how the .asp file 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 Web server and have it processed by ASP.


Create the Form

We have created a form to request user information; you can find it in the file Form.htm in the Tutorial directory (<%= VirtFilePath%>):

<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 VALUE="Submit"><INPUT TYPE=RESET VALUE="Reset">
</FORM>

</BODY>

</HTML>

Like all HTML 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 names and values submitted by a form.

Create the ASP Response Page

Now it's time to learn how a Web server processes the information it receives from an HTML form. For this lesson we also have created an .asp file called Response.asp that will manipulate and display data received from Form.asp, that is, after you add a few extra script commands.

  1. Use your text editor to open the Response.asp file in the <%= Request.ServerVariables("SERVER_NAME")%> Web server's Tutorial directory (<%=FilePath%>).
  2. Search for the words "Define Scripting Language", underneath this text line copy and paste the following line of script:
  3. <%@ LANGUAGE = "<%=ScriptLanguagePreference%>" %>

    Remember, always add this tag as the first script line in you .asp file so that the Web server knows what language your using to write your script.

  4. Now search for the words "Tutorial Lesson" and then underneath this text line copy and paste the following line of script:
  5. <% If ScriptLanguagePreference = "JScript" Then %>
    if (Request.Form("title").Count == 1){
      Title = Request.Form("title")(1)
    }
    <%Else%>
    <% 
      Title = Request.Form("title") 
    <% End If %>

    If another user has previously completed this portion of the tutorial, this script command will already be in place underneath the "Tutorial Lesson" comment. Paste the copied script over the existing script, or copy an unused version of Response.asp from the Template directory to the Tutorial directory.

    Your form submits three different variables or values to ASP:

    ASP stores information submitted by way of HTML forms in the Forms collection of the Request object. To learn more about forms and objects, see Working with HTML Forms and Built-in ASP Objects.

    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.

  6. Copy and paste the following lines of script following the line you inserted in step 2:
  7. <% If ScriptLanguagePreference = "JScript" Then %>
    else{
      Title = ""
    }
      LastName = Request.Form("lname")
      if (Title == "mr"){
        Response.Write("Mr. " + LastName)
      }
      else{
        if (Title == "ms"){
          Response.Write("Ms. " + LastName)
       }
     
    <%Else%>
    LastName = Request.Form("lname")
    If Title = "mr" Then 
    %>  
    Mr. <%= LastName %>  
    <% ElseIf Title = "ms" Then %> 
    Ms. <%= LastName %> 
    <% End If %>

    If another user has previously completed this portion of the tutorial, these lines of script will already be in place. Paste the copied lines of script over the existing script, or copy an unused version of Response.asp from the Template subdirectory <%=FilePath%>\template) to the Tutorial directory.

    The <%=ScriptLanguagePreference%> <% If ScriptLanguagePreference = "JScript" Then %> if...else <%Else%>If...Then..Else <% End If %> statement performs three 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." Otherwise, the first and last name will be used to address the user. You display the value of a variable by using the expression <%= variable-name %>.


  8. To display both the first and last name if the user did not choose a title, copy and paste the following lines of script following the line you inserted in Step 3:
  9. <% If ScriptLanguagePreference = "JScript" Then %>
    else{
        Response.Write(Request.Form("fname") + " " + LastName)
      }
    }
    <%Else%>
    <% Else %>
    <%= Request.Form("fname") & " " & LastName %>
    <% End If %> 
    <% End If %>

    Again, if another user has previously completed this portion of the tutorial, these lines of script will already be in place. Paste the copied lines of script over the existing script, or copy an unused version of Response.asp from the Template subdirectory <%=FilePath%>\template) to the Tutorial directory.

    The <% If ScriptLanguagePreference = "JScript" Then %>plus signs (+) join <%Else%>ampersand (&) joins <% End If %> the values of the variables into one string. The <% If ScriptLanguagePreference = "JScript" Then %>final } curly bracket <%Else%>End If statement <% End If %> ends the conditional statement.

  10. Save your changes to Response.asp and exit the text editor. Be sure your text editor does not replace the .asp file extension.
  11. To verify that the form you've created works (after which you can return to this tutorial by clicking the Back button in your browser), point your browser to /iishelp/iis/htm/tutorial/form.htm">http://<%= Request.ServerVariables("SERVER_NAME")%>/iishelp/iis/htm/tutorial/form.htm

Congratulations! You have activated your first HTML form for sending results to an .asp file. To learn about ActiveX server components, go on to Module 2: Using ActiveX Components.


© 1997 Microsoft Corporation. All rights reserved.