Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

ASP Programming for the Absolute Beginner

.pdf
Скачиваний:
64
Добавлен:
17.08.2013
Размер:
7.66 Mб
Скачать
been added.
file has, indeed,
included in this
asked to be
The line of text you
FIGURE 3.7

56

ASP Programming for the Absolute Beginner

Y L F M A like. However, you shouldEdecide where you will allow the file to be created or

otherwise manipulated,Taccording to the sensitivity of the information being passed and where within your system you want to allow this type of access.

3. After you create a file via the CreateTextFile method of the File object, you write to the newly created file. his is done in the line

TFileStream.WriteLine "Welcome to the File Object in ASP!"

As you see in Figure 3.7, this very line ("Welcome to the File Object in ASP!") is written to the TestFile.txt file. The WriteLine method is a method of the TextStream object, which is discussed in more detail in the following sections.

4.Finally, when the file manipulation is completed, this particular instance of the File object is closed in the line

TFileStream.Close

TRICK

Closing your objects when you are finished working with them is a good practice

 

 

because it helps prevent coding errors and other problems. Leaving an object open

 

after you are done with it is not a good programming practice.

This is a simple example to whet your appetite for working with the File object. Now that you have some idea of how it works, you can dive in to far more robust coding. The first step is a closer examination of the TextStream object.

Introducing the TextStream Object

The TextStream object is the heart of most of the file manipulation in ASP, as illustrated in Listing 3.7. There are three specific methods within the TextStream object: CreateTextFile,

OpenTextFile, and OpenAsTextStream. In Listing 3.7, you use the CreateTextFile to create the file

and the WriteLine method of the TextStream object to write to the newly created file.

Team-Fly®

The TextStream object has its own set of properties and methods. First, take a look at the TextStream object’s properties:

AtEndOfLine. Returns a value of True if the file pointer (the location of the response as the file is read) is at the end of a line in the file.

AtEndOfStream. Returns a value of True if the file pointer is at the end of the file.

Column. Depending on the current character being read within the file, returns the

column number of this character, starting from 1.

Line. Depending on the current line number being read within the file, returns this line number, starting from 1.

You will see many of the TextStream object’s methods in action in the listings and the ASP MadLibs game:

Close. Closes an open file.

Read (a number). Reads a specific number of characters from the file. For example, Read(20) reads the first 20 characters within the file being examined.

ReadAll(). Reads the entire file and places it within a text string. (For more informa-

tion on text strings, see Chapter 2, “Programming ASP Web Pages with VBScript.”)

ReadLine(). Reads a single line from the file and places it within a text string.

Skip (a number). Skips over a specific set of characters from the file being examined.

SkipLine. Skips a line when reading from the file being examined.

Write (a string). Writes a specified string to the file being examined. For example

(as shown in Listing 3.7), the line

TFileStream.WriteLine "Welcome to the File Object in ASP!"

writes the line Welcome to the File Object in ASP! to the specified text file (as specified by the CreateTextFile method, by the way).

WriteLine (a string). Writes a string to the file being examined and then writes a newline character within the file.

WriteBlankLines (a number). Writes the specified number of blank lines to the file being examined.

Although these methods are self-explanatory, it is easier to understand them if you see them in action. The following sections give examples of how to use these various methods of the TextStream object.

Writing to a Text File

Although Listing 3.7 gives you a simple example of how to create and write to a text file, as well as read from a file, Listing 3.8 is a more substantial demonstration of how to write to a text file.

Listing 3.8 SecondFile.asp

<html>

<title>Working with the File Object</title>

<body>

<b>A second example of working with the File object!</b>

57

C h a p te r

3

W o r k in g

w i t A h S P

O b je c t s

58

ASP Programming for the Absolute Beginner

<hr>

<%

set TestFile=Server.CreateObject("Scripting.FileSystemObject")

set TFileStream=TestFile.CreateTextFile("c:\inetpub\wwwroot\ABG_ASP\ Test2File.txt")

TFileStream.WriteLine "Welcome to the File Object in ASP!"

TFileStream.WriteBlankLines(3)

TFileStream.WriteLine "Between this line and the opening line are three blank lines. These blank lines were inserted using the WriteLine method of the TextStream object. Now, let's write three more blank lines before the next section of text is inserted." TFileStream.WriteBlankLines(3)

TFileStream.WriteLine "Okay, that's better--three more blank lines have just been inserted! I think you probably get the idea of how to use the WriteLine method, so let's move on to more interesting things." TFileStream.Close

%>

</body>

</html>

Now, follow these steps to see this code in action:

1.From the CD-ROM, open the file SecondFile.asp, and save it in your ABG_ASP folder within the wwwroot directory.

2.Open your Web browser of choice, and navigate to this file. When the page loads within your Web browser, it looks like Figure 3.8.

FIGURE 3.8

Not much to look at within the browser, but this doesn’t mean that nothing is happening underneath

the hood!

FIGURE 3.9

All the text you asked to be inserted, including the blank lines, is included within this file.

3.After the page loads, the Test2File.txt file is created within your ABG_ASP folder. Navigate to that folder now, and open this file. It looks like Figure 3.9.

You probably have the hang of writing to a text file. It’s a simple process: Create an instance of the File object, define the file path where you want the file to be created, and write to the file with whatever text you want to include. Now you will move on to reading directly from a file into your ASP Web pages.

TRICK

The information you write to a text file doesn’t have to be static text. You can define

 

 

and assign specific values to variables (which you learned how to do in Chapter 2,

 

“Programming ASP Web Pages with VBScript”) and then, as a result of your own

 

code processing, insert dynamic values into the text files. For example, you ask

 

visitors to your Web site to enter specific information. Then, you have your code

 

process that information and write the results of that processing to a text file.

 

Come to think of it, this is exactly the type of processing the ASP MadLibs game

 

performs. Stay tuned for a complete File object example later in this chapter.

Reading from a Text File

Reading from a file is also very straightforward via the power of the TextStream object. See how this is done in Listing 3.9 with some of the TextStream object’s properties and methods.

Listing 3.9 ThirdFile.asp

<html>

<title>Working with the File Object</title>

<body>

59

C h a p te r

3

W o r k in g

w i t A h S P

O b je c t s

60

ASP Programming for the Absolute Beginner

<b>A third example of working with the File object!</b> <hr>

<%

set TestFile=Server.CreateObject("Scripting.FileSystemObject") set TFileStream=TestFile.OpenTextFile("c:\inetpub\wwwroot\ABG_ASP\ Test2File.txt")

TextFormat=TFileStream.ReadLine

%>

<p>

<font face="Century Gothic" size="5" color="#008000"> <i>

<%=TextFormat%></i>

</font></p>

</body>

</html>

As with the other examples, save this file (it is named ThirdFile.asp on the CD-ROM) into your ABG_ASP folder. Then open it in a Web browser. It looks like Figure 3.10.

As you can see in Figure 3.10, the code from Listing 3.9 first opens the Test2File for reading. Then (through the power of the ReadLine method) the code reads the first line of text from this file, Welcome to the File Object in ASP!. Remember from the description of the ReadLine method, the data returned is set to a string. Keeping this in mind, you set the value of the ReadLine method to the variable TextFormat. Finally, before outputting the contents of this variable to the screen (via the line <%=TextFormat%>), you apply some general HTML formatting. I set the font to Century Gothic, assigned a bigger font size, and italicized it. This formatting gives the text (as read from the file) its unique appearance (refer to Figure 3.10).

FIGURE 3.10

The capability to read from an existing text file enables you to draw on all kinds of additional information resources when developing in ASP.

ASP MadLibs—Working with ASP Objects

You have learned how to work with the Request, Response, and File objects and have explored their respective methods and properties. You had a preview of working with forms and QueryStrings and now understand how to create a text file and have your ASP pages write to and read information from these pages.

With all these skills under your belt, you are ready to program ASP MadLibs. This game draws on all three objects in this chapter and gives you yet another preview of working with forms, as well as a hint of how your ASP pages can interact with a database, such as Microsoft Access. Speaking of integrating your ASP pages with Microsoft Access, you will learn how to do just that in Chapter 5, “Database Access with ADO.” Stay tuned for more exciting programming via the power of ASP.

The ASP MadLibs game is composed of the following three steps:

1.A typical HTML form is used to gather the required information from the user (more on what this information entails in just a moment) and pass it to the (ASP) page that processes the data.

2.The page that processes the user information displays the resulting MadLib and presents the user with options (play again, save the MadLib, and so on).

3.A text file is created that allows the user to save his or her unique MadLib creation as a text file, using a name the user specifies.

Sound cool? It should be, when everything is working. Let’s get to it.

The Game Data Input Form

The first part of the ASP MadLibs game is to build the form that gathers the information from the user. Figure 3.11 illustrates this form.

FIGURE 3.11

This form is used to gather the required information from the user so that a unique ASP MadLib can be created.

61

C h a p te r

3

W o r k in g

w i t A h S P

O b je c t s

62

ASP Programming for the Absolute Beginner

This is a simple HTML form (with the 20, one-line, text box form elements placed into a table for a cleaner appearance). The code for this page is in Listing 3.10. Note the form attributes. The ACTION attribute is set to MadLibProcess.asp—the page that will generate the MadLib, based on the information entered on this form. Also, notice that each oneline text box is numbered and named sequentially (Word1, Word2, Word3, and so on).

Listing 3.10 MadLibHome.asp

<html>

<head>

<title>Welcome to ASP MadLibs!</title> </head>

<body>

<p><font face="Century Gothic" size="5"><i>It's Time to Play...</i></font></p> <p align="center"><font face="Comic Sans MS" color="#FF0000" size="6">ASP MadLibs!</font></p>

<hr>

<p align="left"><font face="Century Gothic">Please enter the requested type of word for each space below:</font></p>

<form method="POST" action="MadLibProcess.asp"> <p>

<table border="1" width="100%" height="139"> <tr>

<td width="25%" align="center" height="23"><input type="text" name="Word1" size="15" value="(A Name)"></td>

<td width="25%" align="center" height="23"><input type="text" name="Word2" size="15" value="(Adjective)"></td>

<td width="25%" align="center" height="23"><input type="text" name="Word3" size="15" value="(Noun)"></td>

<td width="25%" align="center" height="23"><input type="text" name="Word4" size="15" value="(Adjective)"></td>

</tr>

<tr>

<td width="25%" align="center" height="23"><input type="text" name="Word5" size="15" value="(Verb-Past)"></td>

<td width="25%" align="center" height="23"><input type="text" name="Word6" size="15" value="(Noun)"></td>

<td width="25%" align="center" height="23"><input type="text" name="Word7" size="15" value="(Adjective)"></td>

<td width="25%" align="center" height="23"><input type="text" name="Word8" size="15" value="(Verb)"></td>

</tr>

<tr>

<td width="25%" align="center" height="23"><input type="text" name="Word9" size="15" value="(Body Part)"></td>

<td width="25%" align="center" height="23"><input type="text" name="Word10" size="15" value="(Adjective)"></td>

<td width="25%" align="center" height="23"><input type="text" name="Word11" size="15" value="(Clothing Piece)"></td>

<td width="25%" align="center" height="23"><input type="text" name="Word12" size="15" value="(Noun)"></td>

</tr>

<tr>

<td width="25%" align="center" height="23"><input type="text" name="Word13" size="15" value="(Noun)"></td>

<td width="25%" align="center" height="23"><input type="text" name="Word14" size="15" value="(Verb-Past)"></td>

<td width="25%" align="center" height="23"><input type="text" name="Word15" size="15" value="(Adjective)"></td>

<td width="25%" align="center" height="23"><input type="text" name="Word16" size="15" value="(Adjective)"></td>

</tr>

<tr>

<td width="25%" align="center" height="17"><input type="text" name="Word17" size="15" value="(Clothing Piece)"></td>

<td width="25%" align="center" height="17"><input type="text" name="Word18" size="15" value="(Body Part)"></td>

<td width="25%" align="center" height="17"><input type="text" name="Word19" size="15" value="(Body part)"></td>

<td width="25%" align="center" height="17"><input type="text" name="Word20" size="15" value="(A Place)"></td>

</tr>

</table>

<hr>

<p align="center"><input type="submit" value="Create My ASP MadLib Now!" name="B1"></p>

</form>

</body>

</html>

The MadLib Processing Page

63

C h a p te r

3

W o r k in g

w i t A h S P

O b je c t s

Now that you have a method (the Web form) for gathering information from the user, it is time to create the page that takes that information and creates the ASP MadLib.

64 The processing page for the game is named MadLibProcess.asp, and the code is in Listing 3.11.

ASP Programming for the Absolute Beginner

Listing 3.11 MabLibProcess.asp

<html>

<head>

<title>Here's Your ASP MadLib!</title> </head>

<body>

<p><font face="Century Gothic" size="5"><i>Thank you!  And now, here is your </i></font></p>

<p align="center"><font face="Comic Sans MS" color="#FF0000" size="6">ASP MadLib!</font></p>

<hr>

<p align="left"><font face="Comic Sans MS" size="3">Dear <font color="#FF0000"><%=Request.Form("Word1")%> ,</font></p>

<p align="left"><font face="Comic Sans MS" size="3">I am having the most interesting vacation.  I've seen so many strange things.  Upon my arrival at the airport, a <font color="#FF0000"><%=Request.Form("Word2")%> </font>

man and I

mixed up our luggage.  At the hotel, I opened his suitcase to find three <font color="#FF0000"><%=Request.Form("Word3")%>s</font>

and a <font color="#FF0000"><%=Request.Form("Word4")%></font> toothbrush.  I nearly

<font color="#FF0000"><%=Request.Form("Word5")%></font> out of there when I discovered a

<font color="#FF0000"><%=Request.Form("Word6")%></font> wrapped in tissue paper.  I took the

<font color="#FF0000"><%=Request.Form("Word7")%></font> man's luggage down to the hotel clerk.  The clerk began to

<font color="#FF0000"><%=Request.Form("Word8")%></font> when I told him about the mix-up.  He pointed his

<font color="#FF0000"><%=Request.Form("Word9")%></font> at a <font color="#FF0000"><%=Request.Form("Word10")%></font> man standing in the

lobby, wearing my <font color="#FF0000"><%=Request.Form("Word11")%></font> on his head!</font></p>

<p align="left"><font face="Comic Sans MS" size="3">After straightening out the luggage mix-up, I decided to do some sight-seeing.  I hardly had made it out of the hotel when I immediately noticed the sky darkening, and it began to

rain <font color="#FF0000"><%=Request.Form("Word12")%>s</font>.  I ran for cover into the closest <font color="#FF0000"> <%=Request.Form("Word13")%></font>

I could

find, but I <font color="#FF0000"><%=Request.Form("Word14")%></font> into an <font color="#FF0000"><%=Request.Form("Word15")%></font>

group of school children.  They began laughing and pointing at me.  </font></p>

<p align="left"><font face="Comic Sans MS" size="3">I checked my reflection in a storefront window, only to discover I had the

<font color="#FF0000"><%=Request.Form("Word16")%></font>

man's <font color="#FF0000"><%=Request.Form("Word17")%></font> hanging from my <font color="#FF0000"><%=Request.Form("Word18")%></font>!  Wow!  I've only been

<font color="#FF0000"><%=Request.Form("Word19")%></font> for three hours and I'm ready

for a vacation from my vacation!  I figure it can't get much worse.  Oh, no!  It seems the stamp I wanted to use to mail this letter is stuck to my <font color="#FF0000"><%=Request.Form("Word20")%></font>!</font></p> <HR>

<p align="left"><font face="Comic Sans MS" size="3">Would you like to save this ASP MadLib to a text file?</font></p>

<form method="POST" action="MadLibSave.asp">

<p align="left">Please enter a name for this file: <input type="text" name="MadLibName" size="20"></p>

<p align="center"><input type="submit" value="Save this ASP MadLib!" name="B1"></p>

<input type="hidden" name="Word1" value="<%=Request.Form("Word1")%>"><input type="hidden" name="Word10" value="<%=Request.Form("Word10")%>">

<input type="hidden" name="Word11" value="<%=Request.Form("Word11")%>"><input type="hidden" name="Word12" value="<%=Request.Form("Word12")%>"><input type="hidden" name="Word13" value="<%=Request.Form("Word13")%>"><input type="hidden" name="Word14" value="<%=Request.Form("Word14")%>"><input type="hidden" name="Word15" value="<%=Request.Form("Word15")%>"><input type="hidden" name="Word16" value="<%=Request.Form("Word16")%>"><input type="hidden" name="Word17" value="<%=Request.Form("Word17")%>"><input type="hidden" name="Word18" value="<%=Request.Form("Word18")%>"><input type="hidden" name="Word19" value="<%=Request.Form("Word19")%>"><input type="hidden" name="Word2" value="<%=Request.Form("Word2")%>"><input type="hidden" name="Word20" value="<%=Request.Form("Word20")%>"><input type="hidden" name="Word3" value="<%=Request.Form("Word3")%>"><input type="hidden" name="Word4" value="<%=Request.Form("Word4")%>"><input type="hidden" name="Word5" value="<%=Request.Form("Word5")%>"><input type="hidden" name="Word6"

65

C h a p te r

3

W o r k in g

w i t A h S P

O b je c t s