home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / software / pelne / optionp / iis4_05.cab / iiatmd1.asp < prev    next >
Text File  |  1997-10-16  |  19KB  |  390 lines

  1. <%@ Language=VBScript EnableSessionState=False %>
  2. <%
  3.   Response.Expires = 0
  4.   LessonFile = Request.ServerVariables("SCRIPT_NAME")
  5.  
  6.   ScriptLanguagePreference = Request.Cookies("ScriptLanguagePreference")
  7.  
  8.   If ScriptLanguagePreference = "" Then
  9.     ScriptLanguagePreference = Request.QueryString("ScriptLanguagePreference")
  10.     If ScriptLanguagePreference = "" Then
  11.       Response.Redirect "/iishelp/iis/htm/asp/iiselect.asp?LessonFile=" & Server.URLEncode(LessonFile)
  12.     End If
  13.   End If
  14.  
  15.  
  16. 'Determine the physical path for the current page and then remove the
  17. 'name of the file from the path (leaving just the directory).
  18. MainPath = Request.ServerVariables("PATH_TRANSLATED")
  19. Length = Len(MainPath)
  20. i = 0
  21. Do Until (i  = 2)
  22.   Do While (Mid(MainPath, Length, 1) <> "\")
  23.     Length = Length - 1      
  24.   Loop  
  25. i = i + 1
  26. MainPath = left(MainPath, (Length-1)) 
  27. Loop
  28. FilePath = left(MainPath, Length) + "\tutorial"
  29.  
  30.  
  31. 'Determine the virtual path for the current page and then remove the
  32. 'name of the file from the path (leaving just the directory).
  33. MainPath = Request.ServerVariables("PATH_INFO")
  34.  
  35. Length = Len(MainPath)
  36. i = 0
  37. Do Until (i  = 2)
  38.   Do While (Mid(MainPath, Length, 1) <> "/")
  39.     Length = Length - 1      
  40.   Loop  
  41. i = i + 1
  42. MainPath = left(MainPath, (Length-1)) 
  43. Loop
  44. VirtFilePath = left(MainPath, Length) + "/tutorial"
  45.  
  46. %>
  47.  
  48. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
  49. <html><head><title>Module 1: Creating ASP Pages</title>
  50.  
  51.  
  52. <SCRIPT LANGUAGE="JavaScript">
  53.     TempString = navigator.appVersion
  54.     if (navigator.appName == "Microsoft Internet Explorer"){    
  55. // Check to see if browser is Microsoft
  56.         if (TempString.indexOf ("4.") >= 0){
  57. // Check to see if it is IE 4
  58.             document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/coua.css">');
  59.         }
  60.         else {
  61.             document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/cocss.css">');
  62.         }
  63.     }
  64.     else if (navigator.appName == "Netscape") {                        
  65. // Check to see if browser is Netscape
  66.         document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/coua.css">');
  67.     }
  68.     else
  69.         document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/cocss.css">');
  70. </script> 
  71.  
  72.  
  73. <META NAME="DESCRIPTION" CONTENT="This is an Active Server Pages (ASP) tutorial where you can learn some ASP basics by creating your own ASP pages (.asp files). "></head>
  74.  
  75. <body bgcolor="#FFFFFF" text="#000000"><font face="Verdana,Arial,Helvetica">
  76.  
  77.  
  78. <h1><a name="module1">Module 1: Creating ASP Pages</a></h1>
  79.  
  80.  
  81. <p><a href="iiselect.asp?LessonFile=<%= Server.URLEncode(LessonFile)%>"><strong>Choose a scripting language for this lesson.</strong></a></p>
  82.  
  83. <p>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.</p>
  84.  
  85. <p><strong><a name="note"><span style="color: #0000FF"><font color="#0000FF">Important</font> </span></a></strong>   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 <a href="../core/iiwspsc.htm">Setting Web Server Permissions</a>.</p>
  86.  
  87. <hr>
  88.  
  89. <h2><a name="creatingasimpleactivexpage">Lesson 1: Creating a Simple ASP Page</a></h2>
  90.  
  91. <p>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 %>/<em>filename.asp</em>. 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.</p>
  92.  
  93. <p>The following HTML creates a simple page with the words “Hello World!” in a large font:</p>
  94.  
  95. <pre><FONT FACE="courier" size="3"><HTML> 
  96. <BODY>
  97. <FONT SIZE=7> 
  98. Hello World!<BR> 
  99. </FONT> 
  100. </BODY>
  101. </HTML> </font></pre>
  102.  
  103. <p>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.</p>
  104.  
  105. <p>Alternatively, you could use ASP to generate this same content in a more dynamic manner.</p>
  106.  
  107. <h3><a name="createandsaveapage">Create and Save a Page</a></h3>
  108.  
  109. <ol>
  110. <li>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.</li>
  111. <li>Copy and paste the following HTML tags at the beginning of the file:</li>
  112.  
  113. <pre><FONT FACE="courier" size="3"><%@ LANGUAGE = "<%=ScriptLanguagePreference%>" %>
  114. <HTML>
  115. <BODY> </font></pre>
  116.  
  117. <p>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.<!--</p>--><br>
  118.  
  119. <li>Save the document as <FONT FACE="courier" size="3">Hello.asp</font> 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.</li>
  120.  
  121. <p>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.<!--</p>--><br>
  122.  
  123. <li>Start a new line after the <font face="courier" size="3"><BODY></font> tag and copy and paste the following script command: 
  124. <%
  125.   If ScriptLanguagePreference = "JScript" Then
  126. %>
  127. <pre><font face="courier" size="3"><% for (i = 3; i <=7;  i++) { %> </font></pre>
  128.  
  129.  
  130. <%Else%>
  131. <pre><font face="courier" size="3"><% For i = 3 To 7 %> </font></pre>
  132.  
  133.  
  134.  
  135. <% End If %><p>Script commands are enclosed within <font face="courier" size="3"><%</font> and <font face="courier" size="3">%></font> characters (also called <em>delimiters</em>). 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 (<font face="courier" size="3">i</font>) is set to 3. The second time the loop is repeated, the counter is set to 4. The loop is repeated until the counter <%
  136.   If ScriptLanguagePreference = "JScript" Then
  137. %>equals<%Else%>exceeds<% End If %> 7.</p>
  138.  
  139. <li>Press Enter, then copy and paste the following line: </li>
  140.  
  141. <pre><font face="courier" size="3"><FONT SIZE=<% = i %>> </font></pre>
  142.  
  143. <p>Each time through the loop, the font size is set to the current value of the counter variable (<font face="courier" size="3">i</font>). 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.</p>
  144.  
  145. <li>Press Enter, then copy and paste the following lines: </li>
  146.  
  147. <pre><FONT FACE="courier" size="3">Hello World!<BR>
  148.  </FONT>
  149. <%  If ScriptLanguagePreference = "JScript" Then
  150. %><% } %><%Else%><% Next %><% End If %>
  151. </BODY>
  152. </HTML></font></pre>
  153.  
  154. <p><%
  155.   If ScriptLanguagePreference = "JScript" Then
  156. %>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 <strong>Next</strong> command repeats the loop (until the counter exceeds 7).<% End If %></p>
  157.  
  158.  
  159.  
  160. <li>The complete file (Hello.asp) should now contain the following text:</li>
  161.  
  162. <pre><font face="courier" size="3">
  163. <%@ LANGUAGE = "<%=ScriptLanguagePreference%>" %>
  164. <HTML>
  165. <BODY>
  166. <%
  167.   If ScriptLanguagePreference = "JScript" Then
  168. %><% for (i = 3; i <=7;  i++) { %><%Else%><% For i = 3 To 7 %> <% End If %>
  169. <FONT SIZE=<% = i %>> 
  170. Hello World!<BR>
  171. </FONT>
  172. <%  If ScriptLanguagePreference = "JScript" Then
  173. %><% } %><%Else%><% Next %><% End If %>
  174. </BODY>
  175. </HTML></font></pre>
  176.  
  177.  
  178. <li>Save your changes. Be sure to save your file in text format and be sure the file name extension is .asp. </li>
  179.  
  180. <p>Some text editors automatically change the file name extension to .txt when you choose <strong>Text Format </strong> in the <strong>Save</strong> dialog box. If this happens, replace the .txt extension with the .asp extension before you click <strong>Save</strong>.</p>
  181.  
  182.  
  183. <li>Exit your text editor. A browser might not be able to request an HTML page that is open in a text editor. </li>
  184.  
  185. <li>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 <a href="http://<%= Request.ServerVariables("SERVER_NAME")%>/iishelp/iis/htm/tutorial/hello.asp"> http://<%= Request.ServerVariables("SERVER_NAME")%>/iishelp/iis/htm/tutorial/hello.asp</a>. </li>
  186.  
  187. <p>You should see a Web page with “Hello World!” displayed five times, each time in a larger font size.<!--</p>--><br>
  188.  
  189.  
  190. </ol>
  191.  
  192. <p>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 <pre><font size="3" face="courier"><%</font></pre> and <pre><font size="3" face="courier">%> </font></pre> 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 
  193. previously opened page).</p>
  194.  
  195. <hr>
  196.  
  197. <h2><a name="creatinganhtmlform">Lesson 2: Creating an HTML Form</a></h2>
  198.  
  199. <p>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.</p>
  200.  
  201. <p>In this lesson, you will create an ASP page that processes the data a user submits by way of an HTML form.</p>
  202.  
  203. <p>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 <strong><a name="script1">Submit </a></strong> button to send your data to the Web server and have it processed by ASP.</p>
  204.  
  205. <hr>
  206. <!--  #include virtual="/iishelp/iis/htm/tutorial/script1.asp" -->
  207.  
  208.  
  209. <h4><a name="H4_37672054">Create the Form</a></h4>
  210.  
  211. <p>We have created a form to request user information; you can find it in the file Form.htm in the Tutorial directory (<%= VirtFilePath%>):</p>
  212.  
  213. <pre><font face="courier" size="3"><HTML>
  214. <HEAD><TITLE>Order</TITLE></HEAD>
  215.  
  216. <BODY>
  217. <H2>Sample Order Form</H2>
  218. <P>
  219. Please provide the following information, then click Submit:
  220.  
  221. <FORM METHOD="POST" ACTION="response.asp">
  222. <P>
  223. First Name: <INPUT NAME="fname" SIZE="48">
  224. <P>
  225. Last Name: <INPUT NAME="lname" SIZE="48">
  226. <P>
  227. Title: <INPUT NAME="title" TYPE=RADIO VALUE="mr">Mr.
  228. <INPUT NAME="title" TYPE=RADIO VALUE="ms">Ms.
  229.  
  230. <P><INPUT TYPE=SUBMIT VALUE="Submit"><INPUT TYPE=RESET VALUE="Reset">
  231. </FORM>
  232.  
  233. </BODY>
  234.  
  235. </HTML></font></pre>
  236.  
  237.  
  238. <p>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 
  239. variable names and values submitted by a form.</p>
  240.  
  241. <h3><a name="createtheaspresponsepage">Create the ASP Response Page</a></h3>
  242.  
  243. <p>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.</p>
  244.  
  245. <ol>
  246. <li>Use your text editor to open the Response.asp file in the <%= Request.ServerVariables("SERVER_NAME")%> Web server's Tutorial directory (<%=FilePath%>). </li>
  247.  
  248. <li>Search for the words "<em>Define Scripting Language</em>", underneath this text line copy and paste the following line of script: </li>
  249.  
  250. <pre><font size="3" face="courier"><%@ LANGUAGE = "<%=ScriptLanguagePreference%>" %></font></pre>
  251.  
  252. <p>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.<!--</p>--><br>
  253.  
  254. <li>Now search for the words "<em>Tutorial Lesson</em>" and then underneath this text line copy and paste the following line of script: </li>
  255.  
  256. <%
  257.   If ScriptLanguagePreference = "JScript" Then
  258. %><pre><font size="3" face="courier">if (Request.Form("title").Count == 1){
  259.   Title = Request.Form("title")(1)
  260. }</font></pre>
  261.  
  262.  
  263. <%Else%><pre><font face="courier" size="3"><% 
  264.   Title = Request.Form("title") </font></pre>
  265.  
  266.  
  267.  
  268. <% End If %>
  269.  
  270. <p>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.<!--</p>--><br>
  271.  
  272. <p>Your form submits three different variables or values to ASP:<!--</p>--><br>
  273.  
  274. <ul>
  275. <li><pre><font face="courier" size="3">fname</font></pre></li>
  276. <li><pre><font face="courier" size="3">lname</font></pre></li>
  277. <li><pre><font face="courier" size="3">title</font></pre></li>
  278. </ul>
  279.  
  280. <p>ASP stores information submitted by way of HTML forms in the <strong>Forms</strong> collection of the <strong>Request</strong> object. To learn more about forms and objects, see <a href="iiwaform.htm">Working with HTML Forms</a> and <a href="iiwaobb.htm">Built-in ASP Objects</a>. <!--</p>--><br>
  281.  
  282. <p>To retrieve information from the <strong>Request</strong> object, you type the following: 
  283. Request.collection-name ("property-name"). Thus, Request.Form ("title") retrieves mr or  ms, depending on the value the user submitted.<!--</p>--><br>
  284.  
  285. </li>
  286.  
  287. <li>Copy and paste the following lines of script following the line you inserted in step 2: </li>
  288.  
  289. <%
  290.   If ScriptLanguagePreference = "JScript" Then
  291. %><pre><font size="3" face="courier">else{
  292.   Title = ""
  293. }
  294.   LastName = Request.Form("lname")
  295.   if (Title == "mr"){
  296.     Response.Write("Mr. " + LastName)
  297.   }
  298.   else{
  299.     if (Title == "ms"){
  300.       Response.Write("Ms. " + LastName)
  301.    }
  302.  </font></pre>
  303.  
  304.  
  305.  
  306. <%Else%><pre><font face="courier" size="3">LastName = Request.Form("lname")
  307. If Title = "mr" Then 
  308. %>  
  309. Mr. <%= LastName %>  
  310. <% ElseIf Title = "ms" Then %> 
  311. Ms. <%= LastName %> </font></pre>
  312.  
  313.  
  314.  
  315. <% End If %>
  316.  
  317. <p>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.<!--</p>--><br>
  318.  
  319.  
  320. <p>The <%=ScriptLanguagePreference%> <%
  321.   If ScriptLanguagePreference = "JScript" Then
  322. %><strong> if...else</strong>
  323.  
  324.  
  325.  
  326. <%Else%><strong>If...Then..Else</strong>
  327.  
  328.  
  329.  
  330. <% End If %> statement performs three different actions depending on the value of <font face="courier" size="3">Title</font>. If Title is <font face="courier" size="3">mr</font>, the user will be addressed as "Mr." If Title is <font face="courier" size="3">ms</font>, 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 %>.</p><!--</p>--><br>
  331.  
  332. <li>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:</li>
  333.  
  334. <%
  335.   If ScriptLanguagePreference = "JScript" Then
  336. %><pre><font size="3" face="courier">else{
  337.     Response.Write(Request.Form("fname") + " " + LastName)
  338.   }
  339. }</pre></font>
  340.  
  341.  
  342.  
  343. <%Else%><pre><font face="courier" size="3"><% Else %>
  344. <%= Request.Form("fname") & " " & LastName %>
  345. <% End If %> </font></pre>
  346.  
  347.  
  348.  
  349. <% End If %>
  350.  
  351. <p>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.<!--</p>--><br>
  352.  
  353. <p>The <%
  354.   If ScriptLanguagePreference = "JScript" Then
  355. %>plus signs (<font size="3" face="courier">+</font>) join
  356.  
  357.  
  358.  
  359. <%Else%>ampersand (<font face="courier" size="3">&</font>) joins
  360.  
  361.  
  362. <% End If %> the values of the variables into one string. The <%
  363.   If ScriptLanguagePreference = "JScript" Then
  364. %>final <strong>}</strong> curly bracket
  365.  
  366.  
  367.  
  368. <%Else%><strong>End If statement</strong>
  369.  
  370.  
  371.  
  372. <% End If %> ends the conditional statement.<!--</p>--><br>
  373.  
  374. </li>
  375.  
  376. <li>Save your changes to Response.asp and exit the text editor. Be sure your text editor does not replace the .asp file extension.</li>
  377.  
  378. <li>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 <a href="http://<%= Request.ServerVariables("SERVER_NAME")%>/iishelp/iis/htm/tutorial/form.htm">http://<%= Request.ServerVariables("SERVER_NAME")%>/iishelp/iis/htm/tutorial/form.htm</a></li>
  379.  
  380. </ol>
  381.  
  382. <p>Congratulations! You have activated your first HTML form for sending results to an .asp file. To learn about ActiveX server components, go on to <a href="iiatmd2.asp">Module 2: Using ActiveX Components</a>.</p>
  383.  
  384. <hr class="iis" size="1">
  385. <p align=center><a href="../../../common/colegal.htm"><em>© 1997 Microsoft Corporation. All rights reserved.</em></a></p>
  386.  
  387. </font>
  388. </body>
  389. </html>
  390.