1623 lines
69 KiB
HTML
1623 lines
69 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
|
||
<html dir=ltr>
|
||
<head>
|
||
<title>Module 1: Creating ASP Pages</title>
|
||
|
||
<SCRIPT LANGUAGE="JavaScript">
|
||
<!--
|
||
TempString = navigator.appVersion
|
||
if (navigator.appName == "Microsoft Internet Explorer"){
|
||
// Check to see if browser is Microsoft
|
||
if (TempString.indexOf ("4.") >= 0){
|
||
// Check to see if it is IE 4
|
||
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/coua.css">');
|
||
}
|
||
else {
|
||
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/cocss.css">');
|
||
}
|
||
}
|
||
else if (navigator.appName == "Netscape") {
|
||
// Check to see if browser is Netscape
|
||
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/coua.css">');
|
||
}
|
||
else
|
||
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/cocss.css">');
|
||
//-->
|
||
</SCRIPT>
|
||
|
||
<SCRIPT LANGUAGE="VBScript">
|
||
<!--
|
||
Sub Window_OnLoad()
|
||
Dim frmContents
|
||
On Error Resume Next
|
||
If Not Parent Is Nothing Then
|
||
Set frmContents = Parent.Contents
|
||
If Not frmContents Is Nothing Then
|
||
frmContents.Window.TOCSynch_Click
|
||
End If
|
||
End If
|
||
End Sub
|
||
//-->
|
||
</SCRIPT>
|
||
|
||
<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).">
|
||
<META HTTP-EQUIV="Content-Type" content="text/html; charset=Windows-1252">
|
||
</head>
|
||
|
||
<body bgcolor="#ffffff" text="#000000">
|
||
|
||
<font face="Verdana,Arial,Helvetica">
|
||
|
||
<h1><a name="module1">Module 1: Creating ASP Pages</a></h1>
|
||
|
||
<p>
|
||
In Module 1, you create ASP pages (.asp files) using HTML and
|
||
Microsoft® Visual Basic® Scripting Edition (VBScript). This module includes the following lessons:
|
||
</p>
|
||
|
||
<p>
|
||
<ul>
|
||
<li>
|
||
<A href="#writing">Lesson 1: Writing an ASP Page</A> Four examples of how to write and run ASP
|
||
pages using HTML and VBScript.
|
||
</li><li>
|
||
<A href="#submitting">Lesson 2: Submitting Information Using Forms</A> Develop a form on an HTML page so that users can enter their personal data.
|
||
</li><li>
|
||
<A href="#creating">Lesson 3: Creating a Guest Book</A> Use forms to
|
||
gather information from visitors, store their information in a Microsoft Access database, and
|
||
display the database contents on a Web page.
|
||
</li><li>
|
||
<A href="#displaying">Lesson 4: Displaying an Excel Spreadsheet</A> Display
|
||
a Microsoft Excel spreadsheet on a Web page.
|
||
</li>
|
||
</ul>
|
||
|
||
<BR>
|
||
<h2><a name="writing">Lesson 1: Writing an ASP Page</a></h2>
|
||
|
||
<p>
|
||
The best way to learn about ASP is to look at examples; alter integer values, strings, and
|
||
statements you are curious about; and determine what changes occur in the browser.
|
||
</p>
|
||
|
||
<p>
|
||
In this lesson you perform the following tasks:
|
||
</p>
|
||
|
||
<ul>
|
||
<li>
|
||
<b>Example 1 </b>Create, save, and run an ASP page using HTML and VBScript.
|
||
</li><li>
|
||
<b>Examples 2, 3, and 4 </b>Add functionality and logic to your ASP page by using built-in functions and conditional
|
||
script statements.
|
||
</li>
|
||
</ul>
|
||
|
||
|
||
<p>
|
||
VBScript is the default scripting
|
||
language for ASP pages; however, the delimiters are the same for JScript. Use
|
||
angle brackets as delimiters around HTML tags just as you would
|
||
in any .htm page, as follows:
|
||
</p>
|
||
|
||
<code>
|
||
<<i>example</i>></<i>example</i>>
|
||
</code>
|
||
|
||
<p>
|
||
Use percent signs with brackets as delimiters around script code, as follows:
|
||
</p>
|
||
|
||
<code>
|
||
<% <i> example</i> %>
|
||
</code>
|
||
|
||
<p>
|
||
</p>
|
||
|
||
<p>
|
||
You can put many script statements inside one pair of script delimiters, as in the following example:</p>
|
||
|
||
<code>
|
||
<p>
|
||
<font face="MS Gothic">
|
||
<BR> <%
|
||
<BR> 'Create a variable.
|
||
<BR> dim strGreeting
|
||
<BR>
|
||
<BR> 'Set the greeting.
|
||
<BR> strGreeting = "Hello World!"
|
||
<BR>
|
||
<BR> 'Print out the greeting, using the ASP Response object.
|
||
<BR> Response.Write strGreeting & "<BR>"
|
||
<BR>
|
||
<BR> 'Also print out the greeting using the <%= method.
|
||
<BR> %>
|
||
<BR> <%=strGreeting%>
|
||
<BR> </font>
|
||
</p>
|
||
</code>
|
||
|
||
<P>
|
||
|
||
</P>
|
||
|
||
<P>
|
||
This code displays the following text in the browser:
|
||
</P>
|
||
<P>
|
||
<FONT face="MS Gothic"> Hello World!
|
||
<BR> Hello World!</FONT>
|
||
</P>
|
||
|
||
|
||
<p>Here is the previous example using
|
||
JScript:</p>
|
||
<p>
|
||
|
||
<code>
|
||
<%@ Language=JScript %>
|
||
<BR>
|
||
<BR> <font face="MS Gothic">
|
||
<BR> <%
|
||
<BR> //Create a variable.
|
||
<BR> var strGreeting;
|
||
<BR>
|
||
<BR> //Set the greeting.
|
||
<BR> strGreeting = "Hello World!";
|
||
<BR>
|
||
<BR> //Print out the greeting, using the ASP Response object.
|
||
<BR> Response.Write(strGreeting + "<BR>");
|
||
<BR>
|
||
<BR> //Also print out the greeting using the <%= method.
|
||
<BR> %>
|
||
<BR> <%=strGreeting%>
|
||
<BR> </font>
|
||
</code>
|
||
<p>
|
||
To create ASP pages, use a text editor such as Notepad and save the page with the .asp extension instead of .htm. The .asp filename extension tells IIS to send the page through the ASP engine before sending the page to a client. (<b>Note:</b> In the Notepad <b>Save As</b> dialog box, when <B>Text Documents (*.txt)</B> is selected in the <B>Save as type</B> box, Notepad automatically appends .txt to the filename. To prevent this, select <B>All Files</B> in the <b>Save as type</b> box, type the full filename <b><I>MyFile</I>.asp</b> in the <b>File Name</b> field, and then click <b>Save</b>.)
|
||
</P>
|
||
|
||
<p>
|
||
|
||
<br></p>
|
||
<h3>Example 1</h3>
|
||
|
||
<p>
|
||
This example displays a greeting, the date, and the current time. To run this example,
|
||
copy and paste the following code into an empty file and save it in the
|
||
<i>x</i>:\Inetpub\Wwwroot\Tutorial directory as <B>Example1.asp</B>. View the example with your browser by typing <b>
|
||
http://localhost/Tutorial/Example1.asp</b> in the address bar.
|
||
</p>
|
||
|
||
|
||
|
||
<code>
|
||
<p>
|
||
<%@ Language=VBScript %>
|
||
<BR>
|
||
<BR> <html>
|
||
<BR> <head>
|
||
<BR> <title>Example 1</title>
|
||
<BR> </head>
|
||
<BR> <body>
|
||
<BR> <font face="MS Gothic">
|
||
<BR>
|
||
<BR> <H1>Welcome to my Home Page</H1>
|
||
<BR> <%
|
||
<BR> 'Create some variables.
|
||
<BR> dim strDynamicDate
|
||
<BR> dim strDynamicTime
|
||
<BR>
|
||
<BR> 'Get the date and time.
|
||
<BR> strDynamicDate = Date()
|
||
<BR> strDynamicTime = Time()
|
||
<BR>
|
||
<BR> 'Print out a greeting, depending on the time, by comparing the last 2 characters in strDymamicTime to "PM".
|
||
<BR> If "PM" = Right(strDynamicTime, 2) Then
|
||
<BR> Response.Write "<H3>Good Afternoon!</H3>"
|
||
<BR> Else
|
||
<BR> Response.Write "<H3>Good Morning!</H3>"
|
||
<BR> End If
|
||
<BR> %>
|
||
<BR> Today's date is <%=strDynamicDate%> and the time is <%=strDynamicTime%>
|
||
<BR>
|
||
<BR> </font>
|
||
<BR> </body>
|
||
<BR> </html>
|
||
</P>
|
||
</code>
|
||
|
||
<P>
|
||
In the browser, you should see something like the following (depending on the date and time you perform this exercise):
|
||
</P>
|
||
<font face="MS Gothic">
|
||
<P>
|
||
<H1> Welcome to my Home Page</H1>
|
||
<H3> Good Afternoon!</H3>
|
||
Today's date is 10/20/2000 and the time is 7:29:50 PM
|
||
</font>
|
||
<p></p>
|
||
<br>
|
||
<p>
|
||
</p>
|
||
<p><b>Note:</b> The Web server processes Example1.asp in the following sequence:
|
||
</p>
|
||
|
||
<ol>
|
||
<li>
|
||
<b><%@ Language=VBScript %></b> tells the ASP engine to use the VBScript engine to translate the script code.</li>
|
||
<li>
|
||
The ASP engine ignores the HTML code blocks.
|
||
</li>
|
||
<li>
|
||
The ASP engine executes the code in the <b><%...%></b> blocks and replaces the
|
||
blocks with placeholders. The results of the
|
||
<b>Response.Write</b> strings and <i><%=...%></i>
|
||
strings are saved in memory on the server.</li>
|
||
<li>
|
||
The results of the
|
||
<b>Response.Write</b> strings and <i><%=...%></i>
|
||
strings are injected
|
||
into the HTML code at the matching placeholders before the page leaves the ASP engine.
|
||
</li>
|
||
<li>
|
||
The complete page leaves the ASP engine as a file of HTML code, and the server
|
||
sends the page to the client.
|
||
</li>
|
||
</ol>
|
||
<BR><h3>Example 2</h3>
|
||
|
||
<p>
|
||
This example incorporates a
|
||
<b>For...Next</b>
|
||
loop in the ASP page to add a little dynamic logic. The <b> For...Next</b> loop is one of six conditional
|
||
statements available to you. The others are
|
||
<b>Do...Loop</b>,
|
||
<b>For Each...Next</b>,
|
||
<b>If...Then...Else...End If</b>,
|
||
<b>Select..Case...End Select</b>, and
|
||
<b>While...Wend</b>. These statements are documented at <a href="http://go.microsoft.com/fwlink/?LinkId=1758" target="_blank">Windows
|
||
Script Technologies</a> under VBScript.
|
||
</p>
|
||
|
||
<P>
|
||
Copy and paste the following code in your text editor, and save the file as
|
||
<B>Example2.asp</B>. View the example with your browser by typing
|
||
<B>http://localhost/Tutorial/Example2.asp
|
||
</B>in the address bar.
|
||
</p>
|
||
|
||
<P>
|
||
The processing order is the same as in Example1.asp.
|
||
</p>
|
||
|
||
<code>
|
||
<p>
|
||
<%@ Language=VBScript %>
|
||
<BR>
|
||
<BR> <html>
|
||
<BR> <head>
|
||
<BR> <title>Example 2</title>
|
||
<BR> </head>
|
||
<BR> <body>
|
||
<BR> <font face="MS Gothic">
|
||
<BR>
|
||
<BR> <%
|
||
<BR> 'Create a variable.
|
||
<BR> dim strTemp
|
||
<BR> dim font1, font2, font3, font, size
|
||
<BR>
|
||
<BR> 'Set the variable.
|
||
<BR> strTemp= "BUY MY PRODUCT!"
|
||
<BR> fontsize = 0
|
||
<BR>
|
||
<BR> 'Print out the string 5 times using the For...Next loop.
|
||
<BR> For i = 1 to 5
|
||
<BR>
|
||
<BR> 'Close the script delimiters to allow the use of HTML code and <%=...
|
||
<BR> %>.
|
||
<BR> <table align=center><font size= <%=fontsize%>> <%=strTemp%> </font></table>
|
||
<BR> <%
|
||
<BR> fontsize = fontsize + i
|
||
<BR>
|
||
<BR> Next
|
||
<BR>
|
||
<BR> %>
|
||
<BR> <table align=center><font size=6><B> IT ROCKS! <B></font></table><BR>
|
||
<BR>
|
||
<BR> </font>
|
||
<BR> </body>
|
||
<BR> </html>
|
||
</P>
|
||
</code>
|
||
|
||
<P>
|
||
In the browser, you should see something like the following:
|
||
</P>
|
||
|
||
<P>
|
||
<FONT face="MS Gothic">
|
||
<TABLE align=center><FONT size=0>BUY MY PRODUCT! </FONT></TABLE>
|
||
<TABLE align=center><FONT size=1>BUY MY PRODUCT! </FONT></TABLE>
|
||
<TABLE align=center><FONT size=3>BUY MY PRODUCT! </FONT></TABLE>
|
||
<TABLE align=center><FONT size=6>BUY MY PRODUCT! </FONT></TABLE>
|
||
<TABLE align=center><FONT size=10>BUY MY PRODUCT! </FONT></TABLE>
|
||
<TABLE align=center><FONT size=6><B>IT ROCKS! </B></FONT></TABLE>
|
||
</FONT>
|
||
<P>
|
||
|
||
<p>Here is Example 2 using JScript:</p>
|
||
<p>
|
||
|
||
<code>
|
||
<%@ Language=JScript %>
|
||
<BR>
|
||
<BR>
|
||
<html><br>
|
||
<head><br>
|
||
<title>Example 2</title><br>
|
||
</head><br>
|
||
<body><br>
|
||
<font face="MS Gothic"><br>
|
||
<br>
|
||
<%<br>
|
||
//Create a variable.<br>
|
||
var strTemp;<br>
|
||
var font1, font2, font3, font, size;<br>
|
||
<br>
|
||
//Set the variable.<br>
|
||
strTemp= "BUY MY PRODUCT!";<br>
|
||
fontsize = 0;<br>
|
||
<br>
|
||
//Print out the string 5 times using the For...Next loop.<br>
|
||
for (i = 1; i < 6; i++) {<br>
|
||
<br>
|
||
//Close the script delimiters to allow the use of HTML code and <%=...<br>
|
||
%>.<br>
|
||
<table align=center><font size= <%=fontsize%>> <%=strTemp%> </font></table><br>
|
||
<%<br>
|
||
fontsize = fontsize + i;<br>
|
||
<br>
|
||
}<br>
|
||
<br>
|
||
%><br>
|
||
<table align=center><font size=6><B> IT ROCKS! <B></font></table><BR><br>
|
||
<br>
|
||
</font><br>
|
||
</body><br>
|
||
</html></code>
|
||
|
||
</p>
|
||
|
||
<h3>Example 3</h3>
|
||
<p>There are more multilingual Web sites every day, as businesses see
|
||
the need to offer their products around the world. Formatting your date, time, and
|
||
currency to match the user's locale is good for diplomacy.
|
||
|
||
<p>
|
||
In Example 3, a predefined function displays the date and currency on your ASP page. The date and currency are formatted for different locales using the
|
||
<b>GetLocale</b> function,
|
||
the <b>SetLocale</b> function,
|
||
the <b>FormatCurrency</b> function,
|
||
and the <b>FormatDateTime</b> function.
|
||
Locale identification strings are listed in the
|
||
<b>Locale ID Chart</b> on MSDN. (This example doesn't cover changing the CodePage to display non-European
|
||
characters on European operating systems. Please read CodePage topics in your IIS Documentation for more information.)
|
||
|
||
<p>
|
||
<b>Note: </b>There are more than 90 predefined functions in VBScript, and they are all
|
||
well-defined at <a href="http://go.microsoft.com/fwlink/?LinkId=1758" target="_blank">Windows
|
||
Script Technologies</a>. To view the documentation, select <B>VBScript</B>,
|
||
select <B> Documentation</B>, select <B> Language Reference</B>,
|
||
and select <B>Functions</B>.
|
||
</p>
|
||
|
||
<p>
|
||
Copy and paste the following code in your text editor, and save the file as
|
||
<B>Example3.asp</B> in the <i>x</i>:\Inetpub\Wwwroot\Tutorial directory. View the example with your browser by typing <B>
|
||
http://localhost/Tutorial/Example3.asp
|
||
</B>in the address bar.
|
||
</p>
|
||
|
||
<code>
|
||
<p>
|
||
<%@ Language=VBScript %>
|
||
<BR>
|
||
<BR> <html>
|
||
<BR> <head>
|
||
<BR> <title>Example 3</title>
|
||
<BR> </head>
|
||
<BR> <body>
|
||
<BR> <font face="MS Gothic">
|
||
<BR>
|
||
<BR> <H3>Thank you for your purchase. Please print this page for your records.</H3>
|
||
<BR> <%
|
||
<BR> 'Create some variable.
|
||
<BR> dim saveLocale
|
||
<BR> dim totalBill
|
||
<BR>
|
||
<BR> 'Set the variables.
|
||
<BR> saveLocale = GetLocale
|
||
<BR> totalBill = CCur(85.50)
|
||
<BR>
|
||
<BR> 'For each of the Locales, format the date and the currency
|
||
<BR> SetLocale("fr")
|
||
<BR> Response.Write"<B>Formatted for French:</B><BR>"
|
||
<BR> Response.Write FormatDateTime(Date, 1) & "<BR>"
|
||
<BR> Response.Write FormatCurrency(totalBill) & "<BR>"
|
||
<BR> SetLocale("de")
|
||
<BR> Response.Write"<B>Formatted for German:</B><BR>"
|
||
<BR> Response.Write FormatDateTime(Date, 1) & "<BR>"
|
||
<BR> Response.Write FormatCurrency(totalBill) & "<BR>"
|
||
<BR> SetLocale("en-au")
|
||
<BR> Response.Write"<B>Formatted for English - Australia:</B><BR>"
|
||
<BR> Response.Write FormatDateTime(Date, 1)& "<BR>"
|
||
<BR> Response.Write FormatCurrency(totalBill) & "<BR>"
|
||
<BR>
|
||
<BR> 'Restore the original Locale
|
||
<BR> SetLocale(saveLocale)
|
||
<BR> %>
|
||
<BR>
|
||
<BR> </font>
|
||
<BR> </body>
|
||
<BR> </html>
|
||
</p>
|
||
</code>
|
||
|
||
<P>
|
||
In the browser, you should see the following:
|
||
</P>
|
||
|
||
<p><H3><FONT face="MS Gothic">Thank you for your purchase. Please print this page for your records.</H3>
|
||
<B> Formatted for French:</B>
|
||
<BR> vendredi 20 octobre 2000
|
||
<BR> 85,50 F
|
||
<BR><B> Formatted for German:</B>
|
||
<BR> Freitag, 20. Oktober 2000
|
||
<BR> 85,50 DM
|
||
<BR><B> Formatted for English - Australia:</B>
|
||
<BR> Friday, 20 October 2000
|
||
<BR> $85.50
|
||
</FONT>
|
||
|
||
<h3>Example 4</h3>
|
||
|
||
<p>
|
||
The most common functions used in ASP Scripts are those that manipulate strings.
|
||
The most powerful string functions use regular expressions. Because regular expressions are difficult to adapt to, Example 4 shows you how to replace characters in a string by using a string
|
||
expression and a regular expression. Regular expressions are defined at <a href="http://go.microsoft.com/fwlink/?LinkId=1758" target="_blank">Windows
|
||
Script Technologies</a>.
|
||
To view the documentation, select <B>VBScript</B>, select <B> Documentation</B>, and select <B>Regular Expressions Guide</B>.
|
||
</p>
|
||
|
||
<p>
|
||
Copy and paste the following code in your text editor, and save the file as <B>Example4.asp</B> in the <i>x</i>:\Inetpub\Wwwroot\Tutorial directory. View the example with your browser by typing <B>
|
||
http://localhost/Tutorial/Example4.asp
|
||
</B>in the address bar.
|
||
</p>
|
||
|
||
<code>
|
||
<p>
|
||
<%@ Language=VBScript %>
|
||
<BR>
|
||
<BR> <html>
|
||
<BR> <head>
|
||
<BR> <title>Example 4</title>
|
||
<BR> </head>
|
||
<BR> <body>
|
||
<BR> <font face="MS Gothic">
|
||
<BR>
|
||
<BR> <H3>Changing a Customer's Street Address</H3>
|
||
<BR> <%
|
||
<BR> 'Create some variables.
|
||
<BR> dim strString
|
||
<BR> dim strSearchFor ' as a string
|
||
<BR> dim reSearchFor ' as a regular expression
|
||
<BR> dim strReplaceWith
|
||
<BR>
|
||
<BR> 'Set the variables.
|
||
<BR> strString = "Jane Doe<BR>100 Orange Road<BR>Orangeville, WA<BR>98100<BR>800.555.1212<BR>"
|
||
<BR> ' Using a string object
|
||
<BR> strSearchFor = "100 Orange Road<BR>Orangeville, WA<BR>98100"
|
||
<BR> ' Using a regular expression object
|
||
<BR> Set reSearchFor = New RegExp
|
||
<BR> reSearchFor.Pattern = "100 Orange Road<BR>Orangeville, WA<BR>98100"
|
||
<BR> reSearchFor.IgnoreCase = False
|
||
<BR>
|
||
<BR> strReplaceWith = "200 Bluebell Court<BR>Blueville, WA<BR>98200"
|
||
<BR>
|
||
<BR> 'Verify that strSearchFor exists...
|
||
<BR> ' using a string object.
|
||
<BR> If Instr(strString, strSearchFor) Then
|
||
<BR> Response.Write "strSearchFor was found in strString<BR>"
|
||
<BR> Else
|
||
<BR> Response.Write "Fail"
|
||
<BR> End If
|
||
<BR> ' using a regular expression object.
|
||
<BR> If reSearchFor.Test(strString) Then
|
||
<BR> Response.Write "reSearchFor.Pattern was found in strString<BR>"
|
||
<BR> Else
|
||
<BR> Response.Write "Fail"
|
||
<BR> End If
|
||
<BR>
|
||
<BR> 'Replace the string...
|
||
<BR> Response.Write "<BR>Original String:<BR>" & strString & "<BR>"
|
||
<BR> ' using a string object.
|
||
<BR> Response.Write "String where strSearchFor is replaced:<BR>"
|
||
<BR> Response.Write Replace(strString, strSearchFor, strReplaceWith) & "<BR>"
|
||
<BR> ' using a regular expression object.
|
||
<BR> Response.Write "String where reSearchFor is replaced:<BR>"
|
||
<BR> Response.Write reSearchFor.Replace(strString, strReplaceWith) & "<BR>"
|
||
<BR> %>
|
||
<BR>
|
||
<BR> </font>
|
||
<BR> </body>
|
||
<BR> </html>
|
||
</p>
|
||
</code>
|
||
|
||
<P>
|
||
In the browser, you should see the following:
|
||
</P>
|
||
|
||
<p>
|
||
<FONT face="MS Gothic"><H3> Changing a Customer's Street Address</H3>
|
||
strSearchFor was found in strString
|
||
<BR> reSearchFor.Pattern was found in strString
|
||
<BR>
|
||
<BR> Original String:
|
||
<BR> Jane Doe
|
||
<BR> 100 Orange Road
|
||
<BR> Orangeville, WA
|
||
<BR> 98100
|
||
<BR> 800.555.1212
|
||
<BR>
|
||
<BR> String where strSearchFor is replaced:
|
||
<BR> Jane Doe
|
||
<BR> 200 Bluebell Court
|
||
<BR> Blueville, WA
|
||
<BR> 98200
|
||
<BR> 800.555.1212
|
||
<BR>
|
||
<BR> String where reSearchFor is replaced:
|
||
<BR> Jane Doe
|
||
<BR> 200 Bluebell Court
|
||
<BR> Blueville, WA
|
||
<BR> 98200
|
||
<BR> 800.555.1212
|
||
</FONT>
|
||
|
||
<p><BR>
|
||
<h2><a name="submitting">Lesson 2: Submitting Information Using Forms</a></h2>
|
||
|
||
<p>
|
||
A common use of intranet and Internet server applications is to accept user
|
||
input by implementing a form in your Web page. ASP includes the following two collections in
|
||
the <b>Request</b> object to help process form information: the <b>QueryString</b> collection and the
|
||
<b>Form</b>
|
||
collection.</p>
|
||
<p>In this lesson, you create an HTML page that accepts user input
|
||
in an HTML form and sends the user input back to the Web server to the same page. The Web server then displays the user input. Later in this module, you use this
|
||
knowledge about forms to build a guest book application that uses ASP scripting.
|
||
To complete this lesson, you perform the following tasks:
|
||
</p>
|
||
|
||
<ul>
|
||
<li>
|
||
<b>Example 1 </b>Display a selection of button elements in a form.
|
||
</li>
|
||
<li>
|
||
<b>Example 2 </b>Display text box elements in a form, accept the user input from the form, and display the user input on the Web page.
|
||
</li>
|
||
</ul>
|
||
|
||
<h3>Example 1: Buttons</h3>
|
||
|
||
<p>
|
||
Forms can contain many different kinds of elements to help your users enter
|
||
data. In this example, there are five input form elements called buttons. There
|
||
are many types of buttons including <b>RADIO</b> buttons, <b>SUBMIT</b> buttons,
|
||
<b>RESET</b> buttons, <b>CHECKBOX</b> buttons, and <b>TEXT</b> buttons.
|
||
</p>
|
||
<p>After the user enters information in a form, the information needs to be sent
|
||
to your Web application. When a user clicks the button labeled
|
||
"Submit" in your Web page, the form data is sent from the
|
||
client to the Web page that is listed in the <b>ACTION</b> element of
|
||
the form tag. This Web page doesn't need to be the same as the calling page. In this example, the Web page listed in the <b>ACTION</b> element is the same as the calling page, which eliminates the need to call another page.
|
||
</p>
|
||
<p>In this example, <b>METHOD</b>="<b>POST</b>" is used to send data
|
||
from the Web client's browser to the Web server. When you use <b>METHOD</b>="<b>POST</b>" in a form, the user data ends up in the
|
||
<b>Form</b> collection of the <b>Request</b> object.
|
||
</p>
|
||
|
||
<p>
|
||
Copy and paste the following code in your text editor, and save the file as
|
||
<B>Button.asp</B> in the <i>x</i>:\Inetpub\Wwwroot\Tutorial directory. View the example with your browser by typing <B>http://localhost/Tutorial/Button.asp</B> in the address bar.
|
||
</p>
|
||
|
||
<code>
|
||
<p>
|
||
<%@ Language=VBScript %>
|
||
<BR>
|
||
<BR> <html>
|
||
<BR> <head>
|
||
<BR> <title>Button Form</title>
|
||
<BR> </head>
|
||
<BR> <body>
|
||
<BR> <font face="MS Gothic">
|
||
<BR>
|
||
<BR> <FORM NAME="Button Example" METHOD="POST" ACTION="button.asp">
|
||
<BR> <H3>Computer Programming Experience:</H3>
|
||
<BR> <p>
|
||
<BR> <INPUT TYPE="RADIO" NAME= "choice" VALUE="Less than 1"> Less than 1 year.<BR>
|
||
<BR> <INPUT TYPE="RADIO" NAME= "choice" VALUE="1 to 5"> 1-5 years.<BR>
|
||
<BR> <INPUT TYPE="RADIO" NAME= "choice" VALUE="More than 5"> More than 5 years.<BR>
|
||
<BR> </p>
|
||
<BR> <p>
|
||
<BR> <INPUT TYPE="SUBMIT" VALUE="Submit">
|
||
<BR> <INPUT TYPE="RESET" VALUE="Clear Form">
|
||
<BR> </p>
|
||
<BR> </form>
|
||
<BR>
|
||
<BR> <%
|
||
<BR> 'Check to see if input has already been entered.
|
||
<BR> dim strChoice
|
||
<BR> strChoice = Request.Form("choice")
|
||
<BR>
|
||
<BR> If "" = strChoice Then
|
||
<BR> Response.Write "<P>(No input yet.)</P>"
|
||
<BR> Else
|
||
<BR> Response.Write "<P>Your last choice was <B>" & strChoice & "</B></P>"
|
||
<BR> End If
|
||
<BR> %>
|
||
<BR>
|
||
<BR> </font>
|
||
<BR> </body>
|
||
<BR> </html>
|
||
</p>
|
||
</code>
|
||
|
||
<P>
|
||
In the browser, you should see the following:
|
||
</P>
|
||
|
||
<FONT face="MS Gothic">
|
||
<FORM name="Button Example" method=post>
|
||
<H3> Computer Programming Experience:</H3>
|
||
<P>
|
||
<INPUT type=radio value="Less than 1" name=choice> Less than 1 year.
|
||
<BR> <INPUT type=radio value="1 to 5" name=choice> 1-5 years.
|
||
<BR> <INPUT type=radio value="More than 5" name=choice> More than 5 years.
|
||
<BR>
|
||
</P>
|
||
<P>
|
||
<INPUT id=button1 type=button value=Submit name=button1>
|
||
<INPUT id=button2 type=button value="Clear Form" name=button2>
|
||
</P>
|
||
</FORM>
|
||
|
||
<P>
|
||
(No input yet.)
|
||
</P>
|
||
</FONT>
|
||
|
||
<BR>
|
||
<h3>Example 2: Input Form Elements</h3>
|
||
|
||
<p>
|
||
In this example, there are three input form elements called text fields and two input form elements called check boxes. Check boxes differ from option buttons because you can
|
||
select more than one. We still need the default <b>Submit</b> button to send the data back to the server.
|
||
<P>In this example, METHOD=GET is used to send
|
||
data from the Web client's browser to the Web server. When you use METHOD=GET in a form, the user data ends up in the
|
||
<b>QueryString</b> collection of the <b>Request</b> object.
|
||
</p>
|
||
<p>Look at the address bar after you click <b>Submit</b>, and you
|
||
should see the elements of the <b>QueryString</b> displayed at the end of the URL.
|
||
</p>
|
||
|
||
<p>
|
||
Copy and paste the following code in your text editor, and save the file as <B>Text.asp</B> in the <i>x</i>:\Inetpub\Wwwroot\Tutorial directory. View the example with your browser by typing <B>
|
||
http://localhost/Tutorial/Text.asp</B> in the address bar.
|
||
</p>
|
||
|
||
<code>
|
||
<p>
|
||
<%@ Language=VBScript %>
|
||
<BR>
|
||
<BR> <html>
|
||
<BR> <head>
|
||
<BR> <title>Text and Checkbox Form</title>
|
||
<BR> </head>
|
||
<BR> <body>
|
||
<BR> <font face="MS Gothic">
|
||
<BR>
|
||
<BR> <FORM NAME="TextCheckbox Example" METHOD="GET"
|
||
ACTION="text.asp">
|
||
<BR> <H3>Please fill out this form to get information on our
|
||
products:</H3>
|
||
<BR> <p>
|
||
<BR> <table>
|
||
<BR> <tr>
|
||
<BR> <td><font face="MS Gothic">Name
|
||
(required)</td>
|
||
<BR> <td><INPUT TYPE="TEXT" NAME="name"
|
||
VALUE="" SIZE="20" MAXLENGTH="150"></td>
|
||
<BR> </tr><tr>
|
||
<BR> <td><font face="MS Gothic">Company</td>
|
||
<BR> <td><INPUT TYPE="TEXT" NAME="company"
|
||
VALUE="" SIZE="25" MAXLENGTH="150"></td>
|
||
<BR> </tr><tr>
|
||
<BR> <td><font face="MS Gothic">E-mail
|
||
(required)</td>
|
||
<BR> <td><INPUT TYPE="TEXT" NAME="email"
|
||
VALUE="" SIZE="25" MAXLENGTH="150"></td>
|
||
<BR> </tr>
|
||
<BR> </table>
|
||
<BR> </p>
|
||
<BR> <p>
|
||
<BR> Requesting information on our:<BR>
|
||
<BR> <INPUT TYPE="CHECKBOX" NAME= "info"
|
||
VALUE="software">Software<BR>
|
||
<BR> <INPUT TYPE="CHECKBOX" NAME= "info"
|
||
VALUE="hardware">Hardware <BR>
|
||
<BR> </p>
|
||
<BR> <p>
|
||
<BR> <INPUT TYPE="SUBMIT" VALUE="Submit">
|
||
<BR> <INPUT TYPE="RESET" VALUE="Clear Form">
|
||
<BR> </p>
|
||
<BR> </form>
|
||
<BR>
|
||
<BR> <%
|
||
<BR> 'Check to see if input has already been entered.
|
||
<BR> dim strName, strEmail, strCompany, strInfo
|
||
<BR> strName = Request.QueryString("name")
|
||
<BR> strEmail = Request.QueryString("email")
|
||
<BR> strCompany = Request.QueryString("company")
|
||
<BR> strInfo = Request.QueryString("info")
|
||
<BR>
|
||
<BR> 'Display what was entered.
|
||
<BR> If ("" = strName) OR ("" = strEmail) Then
|
||
<BR> Response.Write "<P>(No required input
|
||
entered yet.)</P>"
|
||
<BR> Else
|
||
<BR> Response.Write "<P>Your are " &
|
||
strName
|
||
<BR> If Not ("" = strCompany) Then
|
||
<BR> Response.Write " from " &
|
||
strCompany
|
||
<BR> End If
|
||
<BR> Response.Write ". <BR>Your email address
|
||
is " & strEmail
|
||
<BR> If Not ("" = strInfo) Then
|
||
<BR> Response.Write " and you would
|
||
like information on " & strInfo & ".</P>"
|
||
<BR> End If
|
||
<BR> End If
|
||
<BR> %>
|
||
<BR>
|
||
<BR> </font>
|
||
<BR> </body>
|
||
<BR> </html>
|
||
</p>
|
||
</code>
|
||
|
||
<P>
|
||
In the browser, you should see the following:
|
||
</P>
|
||
|
||
<FONT face="MS Gothic">
|
||
<FORM name="Button Example" method=get>
|
||
<H3> Please fill out this form to get information about our products:</H3>
|
||
<P>
|
||
<TABLE>
|
||
<TR>
|
||
<TD><FONT face="MS Gothic"> Name (required)</FONT></TD>
|
||
<TD><INPUT maxLength=150 name=name></TD></TR>
|
||
<TR>
|
||
<TD><FONT face="MS Gothic"> Company</FONT></TD>
|
||
<TD><INPUT maxLength=150 size=25 name=company></TD></TR>
|
||
<TR>
|
||
<TD><FONT face="MS Gothic"> E-mail (required)</FONT></TD>
|
||
<TD><INPUT size=25 name="email" MAXLENGTH="150"></TD>
|
||
</TR>
|
||
</TABLE>
|
||
</P>
|
||
<P> Requesting information about our:
|
||
<BR> <INPUT type=checkbox value=software name=info>Software
|
||
<BR> <INPUT type=checkbox value=hardware name=info>Hardware
|
||
</P>
|
||
<P> <INPUT id=button3 type=button value=Submit name=button3>
|
||
<INPUT id=button4 type=button value="Clear Form" name=button4>
|
||
</P>
|
||
</FORM>
|
||
<P>
|
||
(No required input
|
||
entered yet.)
|
||
</P>
|
||
</FONT>
|
||
<p>
|
||
<BR><h2><a name="creating">Lesson 3: Creating a Guest Book using a Database</a></h2>
|
||
|
||
<p>This lesson requires that you have Microsoft Access installed on your system and is not supported on 64-bit platforms until Access is developed for 64-bit platforms.</p>
|
||
<p>Lesson 3 develops a guest book application. Guest books allow your Web site
|
||
visitors to leave behind information, such as their names, e-mail addresses,
|
||
and comments. In this lesson, you perform the following tasks after creating an
|
||
Access database:
|
||
</p>
|
||
|
||
<ul>
|
||
<li>
|
||
<b>Example 1</b> Create an ASP page to connect to the database
|
||
using only the ADO <b> Connection</b> object.
|
||
</li>
|
||
<li>
|
||
<b>Example 2</b> Create an ASP page to connect to the database
|
||
using the <b> Connection</b> object and the <b>Command</b> object together.
|
||
</li>
|
||
<li>
|
||
<b>Example 3</b> Create an ASP page to display the guest book
|
||
information from the database in a browser.
|
||
</li>
|
||
</ul>
|
||
|
||
<br><h4>Create an Access Database</h4>
|
||
|
||
<p>
|
||
Create an Access database called <B>GuestBook.mdb</B>, and save it in
|
||
<i>x</i>:\Inetpub\Wwwroot\Tutorial. Create a table in the database called
|
||
<B>GuestBook</B>. Use the <B>Create Table Using Design View</B>
|
||
option in Access to add the following fields and properties:
|
||
</p>
|
||
|
||
<p>
|
||
<table border=1 align=center cellspacing=1 cellpadding=5>
|
||
<tr>
|
||
<td align="left" valign="top"><B>Field Name</B></td>
|
||
<td align="left" valign="top"><B>Data Type</B></td>
|
||
<td align="left" valign="top"><B>Field General Properties</B></td>
|
||
</tr><tr>
|
||
<td align="left" valign="top">FID</td>
|
||
<td align="left" valign="top">AutoNumber</td>
|
||
<td align="left" valign="top">Field Size=Long Integer, <BR>New Values=Increment, <BR>Indexed=Yes(No Duplicates)</td>
|
||
</tr><tr>
|
||
<td align="left" valign="top">FTB1</td>
|
||
<td align="left" valign="top">Text</td>
|
||
<td align="left" valign="top">Field Size=255, <BR>Required=No, <BR>Allow Zero Length=Yes, <BR>Indexed=No</td>
|
||
</tr><tr>
|
||
<td align="left" valign="top">FTB2</td>
|
||
<td align="left" valign="top">Text</td>
|
||
<td align="left" valign="top">Field Size=255, <BR>Required=No, <BR>Allow Zero Length=Yes, <BR>Indexed=No</td>
|
||
</tr><tr>
|
||
<td align="left" valign="top">FTB3</td>
|
||
<td align="left" valign="top">Text</td>
|
||
<td align="left" valign="top">Field Size=255, <BR>Required=No, <BR>Allow Zero Length=Yes, <BR>Indexed=No</td>
|
||
</tr><tr>
|
||
<td align="left" valign="top">FTB4</td>
|
||
<td align="left" valign="top">Text</td>
|
||
<td align="left" valign="top">Field Size=255, <BR>Required=No, <BR>Allow Zero Length=Yes, <BR>Indexed=No</td>
|
||
</tr><tr>
|
||
<td align="left" valign="top">FMB1</td>
|
||
<td align="left" valign="top">Memo</td>
|
||
<td align="left" valign="top">Required=No, <BR>Allow Zero Length=Yes</td>
|
||
</tr>
|
||
</table>
|
||
|
||
<BR><h4>Create an ASP Page to Add Data to Your Access Database</h4>
|
||
|
||
<p>
|
||
Now that you have created a database, you can build an ASP page to
|
||
connect to your database and read incoming data using Microsoft ActiveX® Data Objects
|
||
(ADO). ADO is a collection of objects with methods and properties that can
|
||
manipulate data in almost any type of database. (If you plan to use databases frequently, you should purchase a
|
||
programmer's reference book for ADO. Only the most basic ADO code is illustrated
|
||
in the following examples, enough to open, read in, and write to a database.)
|
||
</p>
|
||
|
||
<p>
|
||
The next two examples produce the same results; however, the first example uses only the
|
||
<b>Connection</b> object, and the second example gives part of the job to the <b>Command</b> object,
|
||
which is much more powerful. Compare both examples to see how the objects become
|
||
connected together. After you are comfortable with the the objects,
|
||
use an ADO programmer's reference to experiment with more methods and
|
||
properties.
|
||
</p>
|
||
|
||
<p>To see an example of an ADO error in your ASP page, try browsing to the page
|
||
after renaming your database, after entering a typing mistake in the
|
||
connection string, or after making the database Read Only.
|
||
</p>
|
||
|
||
<BR><h3> Example 1: Using Only the ADO Connection Object
|
||
|
||
</h3>
|
||
|
||
<p>
|
||
Copy and paste the following code in your text editor, and save the file as
|
||
<B>GuestBook1.asp</B> in the <i>x</i>:\Inetpub\Wwwroot\Tutorial directory. View the example with your browser by typing
|
||
<B>http://localhost/Tutorial/GuestBook1.asp
|
||
</B>in the address bar.
|
||
</p>
|
||
|
||
<code>
|
||
<p>
|
||
<%@ Language=VBScript %>
|
||
<BR>
|
||
<BR> <html>
|
||
<BR> <head>
|
||
<BR> <title>Guest Book Using Connection Object Only</title>
|
||
<BR> </head>
|
||
<BR> <body>
|
||
<BR> <font face="MS Gothic">
|
||
<BR> <h2>Guest Book Using Connection Object Only</h2>
|
||
<BR>
|
||
<BR> <%
|
||
<BR> If Not Request.QueryString("Message") = "True" Then
|
||
<BR> 'No information has been input yet, so provide the form.
|
||
<BR> %>
|
||
<BR> <p>
|
||
<BR> <FORM NAME="GuestBook1" METHOD="GET" ACTION="guestbook1.asp">
|
||
<BR> <table>
|
||
<BR> <tr>
|
||
<BR> <td><font face="MS Gothic">From:</td><td><INPUT TYPE="TEXT" NAME="From"></td>
|
||
<BR> </tr><tr>
|
||
<BR> <td><font face="MS Gothic">E-mail Address:</td><td><INPUT TYPE="TEXT" NAME="EmailAdd"></td>
|
||
<BR> </tr><tr>
|
||
<BR> <td><font face="MS Gothic">CC:</td><td><INPUT TYPE="TEXT" NAME="CC"></td>
|
||
<BR> </tr><tr>
|
||
<BR> <td><font face="MS Gothic">Subject:</td><td><INPUT TYPE="TEXT" NAME="Subject"></td>
|
||
<BR> </tr>
|
||
<BR> </table>
|
||
<BR> Message:<br><TEXTAREA NAME="Memo" ROWS=6 COLS=70></TEXTAREA>
|
||
<BR> </p>
|
||
<BR>
|
||
<BR> <p>
|
||
<BR> <INPUT TYPE="HIDDEN" NAME="Message" VALUE="True">
|
||
<BR> <INPUT TYPE="SUBMIT" VALUE="Submit Information">
|
||
<BR> </FORM>
|
||
<BR> </p>
|
||
<BR> <%
|
||
<BR> Else
|
||
<BR> 'The HIDDEN button above sets the Message variable to True.
|
||
<BR> 'We know now that form data has been entered.
|
||
<BR>
|
||
<BR> 'Get the data from the form. We will be inserting it into the database.
|
||
<BR> 'Access doesn't like some characters, such as single-quotes, so encode the
|
||
<BR> ' data using the HTMLEncode method of the ASP Server object.
|
||
<BR> dim strTB1, strTB2, strTB3, strTB4, strMB1, strCommand
|
||
<BR> strTB1 = Server.HTMLEncode(Request.QueryString("From"))
|
||
<BR> strTB2 = Server.HTMLEncode(Request.QueryString("EMailAdd"))
|
||
<BR> strTB3 = Server.HTMLEncode(Request.QueryString("CC"))
|
||
<BR> strTB4 = Server.HTMLEncode(Request.QueryString("Subject"))
|
||
<BR> strMB1 = Server.HTMLEncode(Request.QueryString("Memo"))
|
||
<BR>
|
||
<BR> 'This is a connection string. ADO uses it to connect to a database through the Access driver.
|
||
<BR> 'It needs the provider name of the Access driver and the name of the Access database.
|
||
<BR> 'Connection strings are slightly different, depending on the provider being used,
|
||
<BR> ' but they all use semicolons to separate variables.
|
||
<BR> 'If this line causes and error, search in your registry for
|
||
<BR> ' Microsoft.JET to see if 4.0 is your version.
|
||
<BR> strProvider = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=C:\InetPub\Wwwroot\Tutorial\guestbook.mdb;"
|
||
<BR>
|
||
<BR> 'This creates an instance of an ADO Connection object.
|
||
<BR> 'There are 4 other ADO objects available to you, each with different methods and
|
||
<BR> 'properties that allow you to do almost anything with database data.
|
||
<BR> Set objConn = server.createobject("ADODB.Connection")
|
||
<BR>
|
||
<BR> 'The Open method of the Connection object uses the connection string to
|
||
<BR> ' create a connection to the database.
|
||
<BR> objConn.Open strProvider
|
||
<BR>
|
||
<BR> 'Define the query.
|
||
<BR> 'There are many types of queries, allowing you to add, remove, or get data.
|
||
<BR> 'This query will add your data into the database, using the INSERT INTO key words.
|
||
<BR> 'Here, GuestBook is the name of the table.
|
||
<BR> 'You need single-quotes around strings here.
|
||
<BR> strCommand = "INSERT INTO GuestBook (FTB1,FTB2,FTB3,FTB4,FMB1) VALUES ('"
|
||
<BR> strCommand = strCommand & strTB1 & "','" & strTB2 & "','" & strTB3 & "','" & strTB4 & "','" & strMB1
|
||
<BR> strCommand = strCommand & "')"
|
||
<BR>
|
||
<BR> 'Execute the query to add the data to the database.
|
||
<BR> objConn.Execute strCommand
|
||
<BR>
|
||
<BR> Response.Write("Thank you! Your data has been added.")
|
||
<BR>
|
||
<BR> End If
|
||
<BR> %>
|
||
<BR>
|
||
<BR> </font>
|
||
<BR> </body>
|
||
<BR> </html>
|
||
</p>
|
||
</code>
|
||
|
||
<P>
|
||
In the browser, you should see the following:
|
||
</P>
|
||
|
||
<p>
|
||
<font face="MS Gothic">
|
||
<h2> Guest Book Using Connection Object Only</h2>
|
||
<p>
|
||
<FORM NAME="guestbook" METHOD="POST">
|
||
<table>
|
||
<tr>
|
||
<td><font face="MS Gothic"> From:</td><td><INPUT TYPE="TEXT" NAME="From"></td>
|
||
</tr><tr>
|
||
<td><font face="MS Gothic"> E-mail Address:</td><td><INPUT TYPE="TEXT" NAME="EmailAdd"></td>
|
||
</tr><tr>
|
||
<td><font face="MS Gothic"> CC:</td><td><INPUT TYPE="TEXT" NAME="CC"></td>
|
||
</tr><tr>
|
||
<td><font face="MS Gothic"> Subject:</td><td><INPUT TYPE="TEXT" NAME="Subject"></td>
|
||
</tr>
|
||
</table>
|
||
Message:<br> <TEXTAREA NAME="Memo" ROWS=6 COLS=70></TEXTAREA>
|
||
<p>
|
||
<INPUT TYPE="BUTTON" VALUE="Submit Information">
|
||
</FORM>
|
||
</font>
|
||
|
||
<BR>
|
||
<h3> Example 2: Using the Connection Object and the Command Object Together</h3>
|
||
|
||
<p>
|
||
Copy and paste the following code in your text editor, and save the file as
|
||
<B>GuestBook2.asp</B> in the <i>x</i>:\Inetpub\Wwwroot\Tutorial directory. View the example with your browser by typing <B>
|
||
http://localhost/Tutorial/GuestBook2.asp
|
||
</B>in the address bar.
|
||
</p>
|
||
|
||
<code>
|
||
<p>
|
||
<%@ Language=VBScript %>
|
||
<BR>
|
||
<BR> <html>
|
||
<BR> <head>
|
||
<BR> <title>Guest Book Using Connection Object and Command Object</title>
|
||
<BR> </head>
|
||
<BR> <body>
|
||
<BR> <font face="MS Gothic">
|
||
<BR> <h2>Guest Book Using Connection Object and Command Object</h2>
|
||
<BR>
|
||
<BR> <%
|
||
<BR> If Not Request.QueryString("Message") = "True" Then
|
||
<BR> 'No information has been input yet, so provide the form.
|
||
<BR> %>
|
||
<BR> <p>
|
||
<BR> <FORM NAME="GuestBook2" METHOD="GET" ACTION="guestbook2.asp">
|
||
<BR> <table>
|
||
<BR> <tr>
|
||
<BR> <td><font face="MS Gothic">From:</td><td><INPUT TYPE="TEXT" NAME="From"></td>
|
||
<BR> </tr><tr>
|
||
<BR> <td><font face="MS Gothic">E-mail Address:</td><td><INPUT TYPE="TEXT" NAME="EmailAdd"></td>
|
||
<BR> </tr><tr>
|
||
<BR> <td><font face="MS Gothic">CC:</td><td><INPUT TYPE="TEXT" NAME="CC"></td>
|
||
<BR> </tr><tr>
|
||
<BR> <td><font face="MS Gothic">Subject:</td><td><INPUT TYPE="TEXT" NAME="Subject"></td>
|
||
<BR> </tr>
|
||
<BR> </table>
|
||
<BR> Message:<br><TEXTAREA NAME="Memo" ROWS=6 COLS=70></TEXTAREA>
|
||
<BR> </p>
|
||
<BR>
|
||
<BR> <p>
|
||
<BR> <INPUT TYPE="HIDDEN" NAME="Message" VALUE="True">
|
||
<BR> <INPUT TYPE="SUBMIT" VALUE="Submit Information">
|
||
<BR> </FORM>
|
||
<BR> </p>
|
||
<BR> <%
|
||
<BR> Else
|
||
<BR> 'The HIDDEN button above sets the Message variable to True.
|
||
<BR> 'We know now that form data has been entered.
|
||
<BR>
|
||
<BR> 'Get the data from the form. We will be inserting it into the database.
|
||
<BR> 'Access doesn't like some characters, such as single-quotes, so encode the
|
||
<BR> ' data using the HTMLEncode method of the ASP Server object.
|
||
<BR> dim strTB1, strTB2, strTB3, strTB4, strMB1
|
||
<BR> strTB1 = Server.HTMLEncode(Request.QueryString("From"))
|
||
<BR> strTB2 = Server.HTMLEncode(Request.QueryString("EMailAdd"))
|
||
<BR> strTB3 = Server.HTMLEncode(Request.QueryString("CC"))
|
||
<BR> strTB4 = Server.HTMLEncode(Request.QueryString("Subject"))
|
||
<BR> strMB1 = Server.HTMLEncode(Request.QueryString("Memo"))
|
||
<BR>
|
||
<BR> 'The Memo data type in the Access database allows you to set the field size.
|
||
<BR> If strMB1 = "" Then
|
||
<BR> iLenMB1 = 255
|
||
<BR> Else
|
||
<BR> iLenMB1 = Len(strMB1)
|
||
<BR> End If
|
||
<BR>
|
||
<BR> 'This is a connection string. ADO uses it to connect to a database through the Access driver.
|
||
<BR> 'It needs the provider name of the Access driver and the name of the Access database.
|
||
<BR> 'Connection strings are slightly different, depending on the provider being used,
|
||
<BR> ' but they all use semicolons to separate variables.
|
||
<BR> 'If this line causes and error, search in your registry for
|
||
<BR> ' Microsoft.JET to see if 4.0 is your version.
|
||
<BR> strProvider = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=C:\InetPub\Wwwroot\Tutorial\guestbook.mdb;"
|
||
<BR>
|
||
<BR> 'This creates an instance of an ADO Connection object.
|
||
<BR> 'There are 4 other ADO objects available to you, each with different methods and
|
||
<BR> 'properties that allow you to do almost anything with database data.
|
||
<BR> Set objConn = server.createobject("ADODB.Connection")
|
||
<BR>
|
||
<BR> 'The Open method of the Connection object uses the connection string to
|
||
<BR> ' create a connection to the database.
|
||
<BR> objConn.Open strProvider
|
||
<BR>
|
||
<BR> 'This creates an instance of an ADO Command object.
|
||
<BR> 'Although you could do most of your work with the Connection object,
|
||
<BR> ' the Command object gives you more control.
|
||
<BR> Set cm = Server.CreateObject("ADODB.Command")
|
||
<BR>
|
||
<BR> 'The ActiveConnection property allows you to attach to an Open connection.
|
||
<BR> 'This is how you link the Connection object above to the Command object.
|
||
<BR> cm.ActiveConnection = objConn
|
||
<BR>
|
||
<BR> 'Define the query.
|
||
<BR> 'There are many types of queries, allowing you to add, remove, or get data.
|
||
<BR> 'This query will add your data into the database, using the INSERT INTO keywords.
|
||
<BR> 'Because we are using the Command object, we need to put our query into the
|
||
<BR> ' CommandText property.
|
||
<BR> 'Here, GuestBook is the name of the table.
|
||
<BR> cm.CommandText = "INSERT INTO GuestBook (FTB1,FTB2,FTB3,FTB4,FMB1) VALUES (?,?,?,?,?)"
|
||
<BR>
|
||
<BR> 'This is where you see the power of the Command object.
|
||
<BR> 'By putting ? marks in the string above, we can use the Parameters collection
|
||
<BR> ' to have ADO fill in the ? with the detailed parameters below.
|
||
<BR> 'cm.CreateParameter formats the parameter for you.
|
||
<BR> 'cm.Parameters.Append appends the parameter to the collection.
|
||
<BR> 'Make sure they are in the same order as (TB1,TB2,TB3,TB4,MB1).
|
||
<BR>
|
||
<BR> Set objparam = cm.CreateParameter(, 200, , 255, strTB1)
|
||
<BR> cm.Parameters.Append objparam
|
||
<BR>
|
||
<BR> Set objparam = cm.CreateParameter(, 200, , 255, strTB2)
|
||
<BR> cm.Parameters.Append objparam
|
||
<BR>
|
||
<BR> Set objparam = cm.CreateParameter(, 200, , 255, strTB3)
|
||
<BR> cm.Parameters.Append objparam
|
||
<BR>
|
||
<BR> Set objparam = cm.CreateParameter(, 200, , 255, strTB4)
|
||
<BR> cm.Parameters.Append objparam
|
||
<BR>
|
||
<BR> Set objparam = cm.CreateParameter(, 201, , iLenMB1, strMB1)
|
||
<BR> cm.Parameters.Append objparam
|
||
<BR>
|
||
<BR> 'Execute the query to add the data to the database.
|
||
<BR> 'Here, the Execute method of the Command object is called instead of
|
||
<BR> ' the Execute method of the Connection object.
|
||
<BR> cm.Execute
|
||
<BR>
|
||
<BR> Response.Write("Thank you! Your data has been added.")
|
||
<BR>
|
||
<BR> End If
|
||
<BR> %>
|
||
<BR>
|
||
<BR> </font>
|
||
<BR> </body>
|
||
<BR> </html>
|
||
</p>
|
||
</code>
|
||
|
||
<P>
|
||
In the browser, you should see the same content as GuestBook1.asp, as follows:
|
||
</P>
|
||
<p>
|
||
<font face="MS Gothic">
|
||
<h2> Guest Book Using Connection Object Only</h2>
|
||
<p>
|
||
<FORM NAME="guestbook" METHOD="POST">
|
||
<table>
|
||
<tr>
|
||
<td><font face="MS Gothic"> From:</td><td><INPUT TYPE="TEXT" NAME="From"></td>
|
||
</tr><tr>
|
||
<td><font face="MS Gothic"> E-mail Address:</td><td><INPUT TYPE="TEXT" NAME="EmailAdd"></td>
|
||
</tr><tr>
|
||
<td><font face="MS Gothic"> CC:</td><td><INPUT TYPE="TEXT" NAME="CC"></td>
|
||
</tr><tr>
|
||
<td><font face="MS Gothic"> Subject:</td><td><INPUT TYPE="TEXT" NAME="Subject"></td>
|
||
</tr>
|
||
</table>
|
||
Message:<br> <TEXTAREA NAME="Memo" ROWS=6 COLS=70></TEXTAREA>
|
||
<p>
|
||
<INPUT TYPE="BUTTON" VALUE="Submit Information">
|
||
</FORM>
|
||
</font>
|
||
<br>
|
||
<h3>Example 3: Display the Database in a Browser</h3>
|
||
|
||
<p>After information is entered in a database, you can use a Web page containing
|
||
another script to view and edit the data. Not much changes in the ADO code,
|
||
except the way you define your query.
|
||
<BR>In the last two examples, you used the <b>INSERT INTO</b> query to add records to
|
||
a database. In this example, you use the <b>SELECT</b> query to choose records from
|
||
a database and display them in the browser. You also use the <b>DELETE</b> query to
|
||
remove records from the database. The only query not used in these examples
|
||
is the <b>UPDATE</b> query, whose syntax is like that of the <b>INSERT INTO</b> query. The
|
||
<b>UPDATE</b> query allows you to change fields in a database.
|
||
</p>
|
||
|
||
<p>
|
||
Copy and paste the following code in your text editor, and save the file as
|
||
<B>ViewGB.asp</B> in the <i>x</i>:\Inetpub\Wwwroot\Tutorial directory. View the example with your browser by typing
|
||
<B>http://localhost/Tutorial/ViewGB.asp</b>
|
||
<BR>in the address bar.
|
||
</p>
|
||
|
||
<code>
|
||
<p>
|
||
<%@ Language=VBScript %>
|
||
<BR>
|
||
<BR> <html>
|
||
<BR> <head>
|
||
<BR> <title>View Guest Book</title>
|
||
<BR> </head>
|
||
<BR> <body>
|
||
<BR> <font face="MS Gothic">
|
||
<BR> <h2>View Guest Book</h2>
|
||
<BR>
|
||
<BR> <%
|
||
<BR> 'Read in any user input. Any of these can be empty.
|
||
<BR> 'By doing this first, we can preserve the user input in the form.
|
||
<BR> dim strTB1, strTB2, strTB3, strTB4, strMB1, strSort, iDelete
|
||
<BR> strTB1 = Server.HTMLEncode(Request.QueryString("From"))
|
||
<BR> strTB2 = Server.HTMLEncode(Request.QueryString("EMailAdd"))
|
||
<BR> strTB3 = Server.HTMLEncode(Request.QueryString("CC"))
|
||
<BR> strTB4 = Server.HTMLEncode(Request.QueryString("Subject"))
|
||
<BR> strMB1 = Server.HTMLEncode(Request.QueryString("Memo"))
|
||
<BR> strSort = Server.HTMLEncode(Request.QueryString("sort"))
|
||
<BR> iDelete = CInt(Request.QueryString("Delete"))
|
||
<BR>
|
||
<BR> 'Because we use this variable, and it might not be set...
|
||
<BR> If "" = strSort Then
|
||
<BR> strSort = "FID"
|
||
<BR> End If
|
||
<BR> %>
|
||
<BR>
|
||
<BR> <p>
|
||
<BR> <FORM NAME="ViewGuestBook" METHOD="GET" ACTION="viewgb.asp">
|
||
<BR> <table>
|
||
<BR> <tr>
|
||
<BR> <td><font face="MS Gothic">Sort by which column:</td>
|
||
<BR> <td><SELECT NAME="sort" SIZE="1">
|
||
<BR> <OPTION VALUE="FID">ID Number</OPTION>
|
||
<BR> <OPTION VALUE="FTB1">Name</OPTION>
|
||
<BR> <OPTION VALUE="FTB2">Email</OPTION>
|
||
<BR> <OPTION VALUE="FTB3">CC</OPTION>
|
||
<BR> <OPTION VALUE="FTB4">Subject</OPTION>
|
||
<BR> <OPTION VALUE="FMB1">Memo</OPTION>
|
||
<BR> </SELECT></td>
|
||
<BR> </tr><tr>
|
||
<BR> <td><font face="MS Gothic">Name Contains:</td>
|
||
<BR> <td><INPUT TYPE="TEXT" NAME="From" VALUE="<%=strTB1%>"></td>
|
||
<BR> </tr><tr>
|
||
<BR> <td><font face="MS Gothic">E-mail Address Contains:</td>
|
||
<BR> <td><INPUT TYPE="TEXT" NAME="EmailAdd" VALUE="<%=strTB2%>"></td>
|
||
<BR> </tr><tr>
|
||
<BR> <td><font face="MS Gothic">CC Contains:</td>
|
||
<BR> <td><INPUT TYPE="TEXT" NAME="CC" VALUE="<%=strTB3%>"></td>
|
||
<BR> </tr><tr>
|
||
<BR> <td><font face="MS Gothic">Subject Contains:</td>
|
||
<BR> <td><INPUT TYPE="TEXT" NAME="Subject" VALUE="<%=strTB4%>"></td>
|
||
<BR> </tr><tr>
|
||
<BR> <td><font face="MS Gothic">Memo Contains:</td>
|
||
<BR> <td><INPUT TYPE="TEXT" NAME="Memo" VALUE="<%=strMB1%>"></td>
|
||
<BR> </tr>
|
||
<BR> </table>
|
||
<BR> <INPUT TYPE="SUBMIT" VALUE="Submit Search Parameters">
|
||
<BR> </p>
|
||
<BR>
|
||
<BR> <%
|
||
<BR> 'Create your connection string, create an instance of the Connection object,
|
||
<BR> ' and connect to the database.
|
||
<BR> strProvider = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=C:\InetPub\Wwwroot\Tutorial\guestbook.mdb;"
|
||
<BR> Set objConn = Server.CreateObject("ADODB.Connection")
|
||
<BR> objConn.Open strProvider
|
||
<BR>
|
||
<BR> 'Define the query.
|
||
<BR> If iDelete = 0 Then
|
||
<BR> 'If the Delete variable is not set, the query is a SELECT query.
|
||
<BR> '* means all fields. ASC means ASCII. % is a wildcard character.
|
||
<BR> strQuery = "SELECT * FROM GuestBook"
|
||
<BR> strQuery = strQuery & " WHERE FTB1 LIKE '%" & strTB1 & "%'"
|
||
<BR> strQuery = strQuery & " AND FTB2 LIKE '%" & strTB2 & "%'"
|
||
<BR> strQuery = strQuery & " AND FTB3 LIKE '%" & strTB3 & "%'"
|
||
<BR> strQuery = strQuery & " AND FTB4 LIKE '%" & strTB4 & "%'"
|
||
<BR> strQuery = strQuery & " AND FMB1 LIKE '%" & strMB1 & "%'"
|
||
<BR> strQuery = strQuery & " ORDER BY " & StrSort & " ASC"
|
||
<BR> Else
|
||
<BR> 'We want to delete a record.
|
||
<BR> strQuery = "DELETE FROM GuestBook WHERE FID=" & iDelete
|
||
<BR> End If
|
||
<BR>
|
||
<BR> 'Executing the SELECT query creates an ADO Recordset object.
|
||
<BR> 'This holds the data you get from the database.
|
||
<BR> Set objRS = objConn.Execute(strQuery)
|
||
<BR>
|
||
<BR> 'Now that you have the database data stored in the Recordset object,
|
||
<BR> ' show it in a table.
|
||
<BR> %>
|
||
<BR>
|
||
<BR> <p>
|
||
<BR> <FORM NAME="EditGuestBook" METHOD="GET" ACTION="viewgb.asp">
|
||
<BR> <table border=1 cellpadding=4 >
|
||
<BR> <%
|
||
<BR> On Error Resume Next
|
||
<BR>
|
||
<BR> If objRS.EOF Then
|
||
<BR> If iDelete = 0 Then
|
||
<BR> Response.Write "<tr><td><font
|
||
face=&quot;MS Gothic&quot;>There are no entries in the database.</font></td></tr>"
|
||
<BR> Else
|
||
<BR> Response.Write "<tr><td><font
|
||
face=&quot;MS Gothic&quot;>Record " & iDelete & " was deleted.</font></td></tr>"
|
||
<BR> End If
|
||
<BR> Else
|
||
<BR>
|
||
<BR> 'Print out the field names using some of the methods and properties
|
||
<BR> ' of the Recordset object.
|
||
<BR> Response.Write "<tr>"
|
||
<BR>
|
||
<BR> 'For each column in the current row...
|
||
<BR> For i = 1 to (objRS.Fields.Count - 1)
|
||
<BR> ' write out the field name.
|
||
<BR> Response.Write "<td><font
|
||
face=&quot;MS Gothic&quot;><B>" & objRS(i).Name & "</B></font></td>"
|
||
<BR> Next
|
||
<BR>
|
||
<BR> Response.Write "<td><font face=&quot;MS
|
||
Gothic&quot;><B>Delete</B></font></td>"
|
||
<BR> Response.Write "</tr>"
|
||
<BR>
|
||
<BR> 'Print out the field data, using some other methods and properties
|
||
<BR> ' of the Recordset object. When you see a pattern in how they are used,
|
||
<BR> ' you can look up others and experiment.
|
||
<BR>
|
||
<BR> 'While not at the end of the records in the set...
|
||
<BR> While Not objRS.EOF
|
||
<BR>
|
||
<BR> Response.Write "<tr>"
|
||
<BR>
|
||
<BR> 'For each column in the current row...
|
||
<BR> For i = 1 to (objRS.Fields.Count - 1)
|
||
<BR> ' write out the data in the field.
|
||
<BR> Response.Write "<td><font
|
||
face=&quot;MS Gothic&quot;>" & objRS(i) & "</font></td>"
|
||
<BR> Next
|
||
<BR>
|
||
<BR> 'Add a button that will pass in an ID number to delete a record.
|
||
<BR> %><td><INPUT TYPE="SUBMIT" NAME="Delete" VALUE="<%=objRS(0)%>"></td><%
|
||
<BR>
|
||
<BR> Response.Write "</tr>"
|
||
<BR>
|
||
<BR> 'Move to the next row.
|
||
<BR> objRS.MoveNext
|
||
<BR>
|
||
<BR> Wend
|
||
<BR>
|
||
<BR> End If 'objRS.EOF
|
||
<BR> %>
|
||
<BR> </table>
|
||
<BR> </FORM>
|
||
<BR>
|
||
<BR> <%
|
||
<BR> 'Close the Connection.
|
||
<BR> objConn.Close
|
||
<BR> %>
|
||
<BR>
|
||
<BR> </font>
|
||
<BR> </body>
|
||
<BR> </html>
|
||
</p>
|
||
</code>
|
||
|
||
<P>
|
||
In the browser, you should see the following:
|
||
</P>
|
||
|
||
<p>
|
||
<font face="MS Gothic">
|
||
<h2> View Guest Book</h2>
|
||
<p>
|
||
<FORM NAME="viewguestbook" METHOD="GET">
|
||
<table>
|
||
<tr>
|
||
<td><font face="MS Gothic"> Sort by which column:</td>
|
||
<td> <SELECT NAME="sort" SIZE="1">
|
||
<OPTION VALUE="FID">ID Number</OPTION>
|
||
<OPTION VALUE="FTB1">Name</OPTION>
|
||
<OPTION VALUE="FTB2">Email</OPTION>
|
||
<OPTION VALUE="FTB3">CC</OPTION>
|
||
<OPTION VALUE="FTB4">Subject</OPTION>
|
||
<OPTION VALUE="FMB1">Memo</OPTION>
|
||
</SELECT></td>
|
||
</tr><tr>
|
||
<td> <font face="MS Gothic">Name Contains:</td>
|
||
<td><INPUT TYPE="TEXT" NAME="From" VALUE=""></td>
|
||
</tr><tr>
|
||
<td> <font face="MS Gothic">E-mail Address Contains:</td>
|
||
<td><INPUT TYPE="TEXT" NAME="EmailAdd" VALUE=""></td>
|
||
</tr><tr>
|
||
<td> <font face="MS Gothic">CC Contains:</td>
|
||
<td><INPUT TYPE="TEXT" NAME="CC" VALUE=""></td>
|
||
</tr><tr>
|
||
<td> <font face="MS Gothic">Subject Contains:</td>
|
||
<td><INPUT TYPE="TEXT" NAME="Subject" VALUE=""></td>
|
||
</tr><tr>
|
||
<td> <font face="MS Gothic">Memo Contains:</td>
|
||
<td><INPUT TYPE="TEXT" NAME="Memo" VALUE=""></td>
|
||
</tr>
|
||
</table>
|
||
<INPUT TYPE="BUTTON" VALUE="Submit Search Parameters">
|
||
</p>
|
||
<p>
|
||
<FORM NAME="editguestbook" METHOD="GET">
|
||
<table border=1 cellpadding=4>
|
||
<tr><td><font face="MS Gothic"> There are no entries in the database.</font></td></tr>
|
||
</table>
|
||
</FORM>
|
||
</font>
|
||
|
||
<P><BR><h2><a name="displaying">Lesson 4: Displaying an Excel Spreadsheet</a></h2>
|
||
<p>This lesson requires that you have Microsoft Excel installed on your system and is not
|
||
supported on 64-bit platforms until Excel is developed for 64-bit platforms.</p>
|
||
<p>This lesson demonstrates how to display a Microsoft Excel spreadsheet in a
|
||
Web page. As in the previous lesson, you use ADO. However, in
|
||
this lesson you connect to an Excel spreadsheet instead of an Access database.
|
||
</p>
|
||
|
||
<H4>Prepare your Excel spreadsheet to display in an Active Server Page</H4>
|
||
|
||
<ol>
|
||
<li>
|
||
Using either Excel 98 or Excel 2000, create a spreadsheet and save it as
|
||
<B>ASPTOC.xls</B> in <i>x</i>:\Inetpub\Wwwroot\Tutorial.
|
||
Do not include any special formatting or column labels when creating the spreadsheet.
|
||
</li><li>
|
||
Fill in some of the fields with random data. Treat the first row of cells
|
||
as column names.
|
||
</li><li>
|
||
Highlight the rows and columns on the spreadsheet that you want to display in
|
||
the Web page. (Click on one of the fields and drag your mouse diagonally to
|
||
highlight a block of fields.)
|
||
</li><li>
|
||
On the <B>Insert</B> menu, select <B>Name</B>, and click <B>Define</B>. This is where all your workbooks are defined by name
|
||
and range of cells.
|
||
</li><li>
|
||
Make sure the cell range you highlighted is displayed correctly at the bottom.
|
||
Type the name <B>MYBOOK</B> for your workbook, and select
|
||
<B>Add</B>. Whenever you change MYBOOK, make sure the correct
|
||
cell range is displayed in the <b>Refers to</b> text box at the bottom of
|
||
the <b>Define Name</b> window. Simply selecting MYBOOK after
|
||
highlighting a new block of cells does not update the cell range.
|
||
</li><li>
|
||
If the name shows up in the workbook list, click <B>OK</B>.
|
||
Save your spreadsheet.
|
||
</li><li>
|
||
Close Excel to remove the lock on the file so that your ASP page can access it.
|
||
</li>
|
||
</ol>
|
||
|
||
<p>
|
||
In the examples in Lesson 3, you specified a provider name in the connection string, which maps to a specific ADO DLL. In this example, you use a driver
|
||
name, which causes ASP to use the default provider for that driver name.
|
||
</p>
|
||
|
||
<p>
|
||
Copy and paste the following code in your text editor, and save the file as
|
||
<B>ViewExcel.asp</B> in the <i>x</i>:\Inetpub\Wwwroot\Tutorial directory. View the example with your browser by typing <B>
|
||
http://localhost/Tutorial/ViewExcel.asp
|
||
</B>in the address bar.
|
||
</p>
|
||
|
||
<code>
|
||
<p>
|
||
<%@ Language=VBScript %>
|
||
<BR>
|
||
<BR> <html>
|
||
<BR> <head>
|
||
<BR> <title>Display an Excel Spreadsheet in a Web Page</title>
|
||
<BR> </head>
|
||
<BR> <body>
|
||
<BR> <font face="MS Gothic">
|
||
<BR> <h2>Display an Excel Spreadsheet in a Web Page</h2>
|
||
<BR>
|
||
<BR> <%
|
||
<BR> 'Create your connection string, create an instance of the Connection object,
|
||
<BR> ' and connect to the database.
|
||
<BR> strDriver = "Driver={Microsoft Excel Driver (*.xls)};DBQ=C:\Inetpub\Wwwroot\Tutorial\MyExcel.xls;"
|
||
<BR> Set objConn = Server.CreateObject("ADODB.Connection")
|
||
<BR> objConn.Open strDriver
|
||
<BR>
|
||
<BR> 'Selects the records from the Excel spreadsheet using the workbook name you saved.
|
||
<BR> strSELECT = "SELECT * from `MYBOOK`"
|
||
<BR>
|
||
<BR> 'Create an instance of the ADO Recordset object, and connect it to objConn.
|
||
<BR> Set objRS = Server.CreateObject("ADODB.Recordset")
|
||
<BR> objRS.Open strSELECT, objConn
|
||
<BR>
|
||
<BR> 'Print the cells and rows in the table using the GetString method.
|
||
<BR> Response.Write "<H4>Get Excel data in one string with the GetString method</H4>"
|
||
<BR> Response.Write "<table border=1 ><tr><td>"
|
||
<BR> Response.Write objRS.GetString (, , "</td><td><font face=&quot;MS Gothic&quot;>", "</font></td></tr><tr><td>", NBSPACE)
|
||
<BR> Response.Write "</td></tr></table>"
|
||
<BR>
|
||
<BR> 'Move to the first record.
|
||
<BR> objRS.MoveFirst
|
||
<BR>
|
||
<BR> 'Print the cells and rows in the table using the ViewGB.asp method.
|
||
<BR> Response.Write "<H4>Get Excel data using MoveNext</H4>"
|
||
<BR>
|
||
<BR> 'Print out the field names using some of the methods and properties
|
||
<BR> ' of the Recordset object.
|
||
<BR> Response.Write "<table border=1 ><tr>"
|
||
<BR>
|
||
<BR> 'For each column in the current row...
|
||
<BR> For i = 0 to (objRS.Fields.Count - 1)
|
||
<BR> ' write out the field name.
|
||
<BR> Response.Write "<td><font face=&quot;MS
|
||
Gothic&quot;><B>" & objRS(i).Name & "</B></font></td>"
|
||
<BR> Next
|
||
<BR>
|
||
<BR> 'While not at the end of the records in the set...
|
||
<BR> While Not objRS.EOF
|
||
<BR>
|
||
<BR> Response.Write "</tr><tr>"
|
||
<BR>
|
||
<BR> 'For each column in the current row...
|
||
<BR> For i = 0 to (objRS.Fields.Count - 1)
|
||
<BR> ' write out the data in the field.
|
||
<BR> %><td><font face="MS Gothic"><%=objRS(i)%></font></td><%
|
||
<BR> Next
|
||
<BR>
|
||
<BR> 'Move to the next row.
|
||
<BR> objRS.MoveNext
|
||
<BR>
|
||
<BR> Wend
|
||
<BR>
|
||
<BR> Response.Write "</tr></table>"
|
||
<BR>
|
||
<BR> 'Close the Connection.
|
||
<BR> objConn.Close
|
||
<BR> %>
|
||
<BR>
|
||
<BR> </font>
|
||
<BR> </body>
|
||
<BR> </html>
|
||
</p>
|
||
</code>
|
||
|
||
<P>
|
||
In the browser, you should see the following:
|
||
</P>
|
||
|
||
<p>
|
||
<font face="MS Gothic">
|
||
<h2> Display an Excel Spreadsheet in a Web Page</h2>
|
||
<H4> Get Excel data in one string with the GetString method</H4>
|
||
<table border=1>
|
||
<tr>
|
||
<td>A2</td><td><font face="MS Gothic">B2</td><td><font face="MS Gothic">C2</font></td>
|
||
</tr><tr>
|
||
<td>A3</td><td><font face="MS Gothic">B3</td><td><font face="MS Gothic">C3</font></td>
|
||
</tr>
|
||
</table>
|
||
<H4> Get Excel data using MoveNext</H4>
|
||
<table border=1>
|
||
<tr>
|
||
<td><font face="MS Gothic"><B>A1</B></font></td><td><font face="MS Gothic"><B>B1</B></font></td><td><font face="MS Gothic"><B>C1</B></font></td>
|
||
</tr><tr>
|
||
<td><font face="MS Gothic">A2</font></td><td><font face="MS Gothic">B2</font></td><td><font face="MS Gothic">C2</font></td>
|
||
</tr><tr>
|
||
<td><font face="MS Gothic">A3</font></td><td><font face="MS Gothic">B3</font></td><td><font face="MS Gothic">C3</font></td>
|
||
</tr>
|
||
</table>
|
||
</font>
|
||
|
||
<BR><H2>Up Next: Using COM Components in ASP Pages</H2>
|
||
|
||
<hr class="iis" size="1">
|
||
<p align=center><A href="/iishelp/common/colegal.htm"><em>© 1997-2001 Microsoft Corporation. All rights reserved.</em></A></p></font>
|
||
</body>
|
||
</html>
|