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

ASP Programming for the Absolute Beginner

.pdf
Скачиваний:
64
Добавлен:
17.08.2013
Размер:
7.66 Mб
Скачать

26

ASP Programming for the Absolute Beginner

FIGURE 2.2

 

Y

A simple ASP page

 

but with enhanced

 

 

 

functionality,

L

compared to the

F

.html version.

 

M

 

between ABC.html and ABC.asp:

A E Aside from the obviousTdifferences in appearance, there are some critical differences

• Note the name of the page in Figure 2.2 and how it has an .asp extension. This indicates that the page is an ASP page.

• Note how, compared to ABC.html (refer to Figure 2.1), the ABC.asp page offers the visitor the ability to receive information about the company immediately via e- mail rather than wait four to six weeks for delivery through regular “snail mail.”

• Take a look at the code behind ABC.asp:

TRICK The use of <%= and <% and other opening/closing VBScript commands and functions

will be discussed in more detail throughout this book. For now, just be sure to recognize these special characters and the fact that they must be included within your code (as shown) in order for your ASP pages to function properly.

Listing 2.2 ABC.asp

<html>

<head>

<title>Welcome to the ABC Company</title> </head>

<body>

<p align="center"><b>Welcome to the ABC Company </b></p>

Team-Fly®

<p align="center"><b>Today is <%=DATE%></b></p> <hr>

<p align="left"><i>Thanks for your interest in the ABC Company!  Complete the form below, and you will immediately receive information about the company via e-mail!</i></p>

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

<p align="left">Your name: <input type="text" name="T1" size="20"></p>

<p align="left">Your e-mail address: <input type="text" name="T2" size="20"></p> <p align="center"><input type="submit" value="Send my information right now!" name="B1"></p>

</form>

<p align="left"> </p>

</body>

</html>

Notice how, compared to ABC.html, the ABC.asp page uses the VBScript function DATE to display the current date on the Web page dynamically. Also, the page uses a form to capture information from the user on the spot so that the user can receive information immediately via e-mail.

By utilizing the DATE function, the ABC.asp page is self-sufficient in that the date displayed is always current and doesn’t need to be manually updated. Remember, in ABC.html, the date is hard-coded, so the HTML used to display the data has to be changed each day to display the correct date.

Although a simple demonstration, this is a prime example of the power and functionality ASP brings to your Web development.

Client-Side versus Server-Side Processing

In the preceding section, you caught a glimpse of how ASP can take your Web pages to an entirely new level, but how does ASP work?

A critical element of ASP is that it executes on the server rather than the client, that is, the Web browser itself. What does this mean? Let me use an analogy to explain.

Imagine that your television goes on the blink. Rather than have it repaired at the shop, you ask the repairman to come out to your house. Although you give good directions to your home, and the repairman doesn’t have a hard time finding you, the story changes considerably after he arrives. He quickly discovers that, because of the specific make and model of your television, he can’t repair it in your home, even with his full set of tools and general skill.

Now, imagine another scenario. In this case, you take your television to the shop to have it repaired. Because the repairman doesn’t need to anticipate any special requirements of your TV or guess which tools he’ll need for the job, he can utilize his full complement of resources, both tools and skill, in repairing your TV. Moreover, because the repair work is being done in his shop, he has full control over how the repair

27

C h a p te r

2

P r o g r am m i n A g S P

W e P b a g e s

w i t h

V B S c ri p t

28

ASP Programming for the Absolute Beginner

proceeds. Not only does he have easy access to all his tools, but he can also use sophisticated testing equipment on your set. Ultimately, the TV is returned to you, repaired correctly and—in this case—more quickly because you didn’t experience the delay of the repairman’s first coming out to your house and discovering that he didn’t have everything he needed.

What does this have to do with ASP? Think of client-side versus server-side processing in the same fashion. When you utilize a language that is processed in the client, for example, JavaScript, you risk the possibility of the repairman—in this analogy, the visitors to your site and their specific Web browser—not being configured to display or manipulate (or incapable of displaying or manipulating) the code you have placed within your Web page. In other words, if the code within your Web page executes in a browser that can’t handle it, for whatever reason, the code malfunctions or, worse, doesn’t display at all. You are then left with a broken TV, to carry out the analogy. However, if you use server-side processing, all the repair work can be taken care of on the server, with the browser client’s only involvement being the receipt of the processed code. This processed code is straight HTML, so the vast majority of browsers can

SHOULD YOU ALWAYS USE SERVER-SIDE PROCESSING?

From the TV repair analogy I’ve presented here, you might think that the only way to make Web pages that utilize a programming language work properly is to use server-side processing. Although it’s true that server-side processing has many advantages, it’s not accurate to say that client-side processing is a bad thing. The real issue in this client-side/server-side processing debate is whether you can safely assume, or control, the type of Web browser that will be used to display your VBScript-enabled Web pages (in this case). For example, VBScript is a Microsoft innovation. It is designed to work, for the most part, exclusively in the Microsoft Web browser Internet Explorer. Now, that statement is true if you use VBScript in the manner of client-side processing or if the VBScript is executed in the Web browser itself. Usually, if you try to execute VBScript directly in other types of browsers, it will not function. If you have the VBScript executed on the Web server, though, the only thing sent to the Web browser client is the results of this execution. These are mostly straight HTML, which is capable of being displayed in any Web browser.

Some programming languages are designed from the onset to work in the client. JavaScript is a fine example of this. However, you need to keep in mind that with even the most universal client-side languages, there can be differences. For example, both Microsoft and Netscape have slightly different methods of interpreting JavaScript. A Web page you create with JavaScript will typically function the same in Internet Explorer and Netscape Navigator, but differences—some big and some small—will creep up when you try to execute JavaScriptenabled pages in the various browsers. Does this mean that you should avoid using them? Not necessarily. However, you should be cognizant of the fact that, like our friendly TV repairman, when you bring the repair into the client’s house (that is, the client-side processing in the browser) versus doing the work at the shop (server-side processing), you introduce a level of unpredictability because you don’t know what kind of environment you will be working in and which tools you will need to get the job done right.

display it. In a nutshell, server-side processing takes all the guesswork and variability out of utilizing a programming language such as VBScript within your Web pages because the code processing is done on the server, not in the browser.

Understanding VBScript Variables

Variables are perhaps the most essential programming elements because they allow you to assign values to specific markers in your code. With this assignment comes the power to further manipulate the values. A variable value can be nearly anything: a telephone number, an e-mail address, an order date, a customer’s name–the list goes on.

Consider the following example:

Listing 2.3 VariableA.html

Email_Address="rsmith@zeppelin.com"

Age=31

UserName="Robert Smith"

You see three variables (Email_Address, Age, and UserName) being assigned specific values (rsmith@zeppelin.com, 31, and Robert Smith, respectively). After variables are assigned, they can be manipulated or otherwise referenced in your code, as in the following sample page, Variable.asp:

Listing 2.4 VariableB.asp

<html>

<head>

<title>Working with Variables</title> </head>

<body>

<% Email_Address="rsmith@zeppelin.com" Age=31

Name="Robert Smith" %>

<b>This is an example of working with variables within ASP</b><hr> The e-mail address is: <%=Email_Address%><p>

The age is: <%=Age%><p> The name is: <%=Name%> </body>

</html>

When this code is executed, it appears as shown in Figure 2.3.

Also, as discussed in the preceding section, because Variable.asp has its code processed on the server, only the results are sent to the browser. When the source code for this page is viewed in the browser, only the processed HTML is visible. In other words, the

29

C h a p te r

2

P r o g r am m i n A g S P

W e P b a g e s

w i t h

V B S c ri p t

30

ASP Programming for the Absolute Beginner

FIGURE 2.3

The VBScript in this page allows variables to be assigned values and then allows those values to be displayed.

FIGURE 2.4

Because the

VBScript in Variable.asp is processed on the server, only the resulting HTML is sent to the browser.

assignment of the variables (the actual VBScript) is processed on the server and therefore not visible to users when they check the source code in their Web browser (see Figure 2.4).

HINT

Because only the resulting HTML is sent to the Web browser, server-side processing is more secure, in general, because a user can’t see potentially sensitive program code when viewing the source code.

In VBScript, there are several subtypes of variables, but I’m going to focus on the following four:

Integer. This subtype can have an assigned value anywhere in the range of -32768 to 32768. Think of an integer subtype as the number variable, where you can assign specific numeric values to the variable names you set. Examples of an integer variable are age=31 or weight=190.

Date. This subtype stores (you guessed it) date values. The value range for the date subtype can range from January 1, 100, to December 31, 9999.

Currency. Useful for storing monetary values. There are some specific formatting concerns with this subtype, as there are with the date subtype. By calling specific VBScript functions (special commands used to execute stored procedures), you can display currency and date values in different ways. You can see a variety of these formatting features illustrated in Appendix A, “VBScript Variable Reference.”

String. This is perhaps the most common subtype. In general, you use string vari-

ables to store textual values, such as FirstName="William Burroughs" or

CityName="Indianapolis".

HINT

Note in the examples that with string variables you use quotation marks around the

 

value assigned to the variable, but with integer subtypes you do not use quotes. I’ll

 

talk more about this difference in the next section.

Declaring Constants in VBScript

Remember your senior prom or your wedding? Before you walked into the reception hall with your date or spouse, you were announced to the crowd so that everyone would know who you are. This is analogous to declaring constants in VBScript.

What does declaring mean, and why do you have to be concerned with it? When you assign a value to a variable, you declare a constant. That is, you assign a permanent value—until you declare it with a different value. The following are examples of declaring constants in VBScript:

LastName="Johnson"

DOB="4/12/70"

Years_Employed=15

As you will see in coding examples throughout this book, variables can have their values changed as your code is executed, depending on how your code functions. This can be useful when you want to use a variable as a temporary holding container for a transient value. For example, you write a program that displays 10 random numbers to the screen. In the program, you use the variable Display_Number in which to assign the random number. However, because you are displaying 10 different numbers, you want Display_Number to be able to have its value changed.

In another section of this program, you want to refer back to the user’s name at several points in the code, via the variable UserName. Because the user’s name doesn’t change,

31

C h a p te r

2

P r o g r am m i n A g S P

W e P b a g e s

w i t h

V B S c ri p t

32

you want to declare a specific value for this UserName variable so that it will not change,

 

unless you change it by assigning a new value to the variable.

ASP Programming for the Absolute Beginner

The simple way to view this is that you are always declaring variables the moment you assign a value to them. However, when you assign a value to a variable that you don’t want to change, you are declaring a constant for that variable.

TRICK

For more specific information on VBScript variable subtypes, including how to ma-

 

 

nipulate them, be sure to refer to Appendix A, “VBScript Variable Reference.”

Learning to Program with VBScript

By all definitions, the following simple example is a VBScript program, which comes alive when placed within an ASP Web page:

Listing 2.5 Simple_Program.asp

<html>

<body>

<%

FirstName="John"

LastName="Gosney"

%>

Welcome to VBScript <%=FirstName%> <%=LastName%>!

I know you are really going to love it!

</body>

</html>

When this code is executed on the Web server, it appears in a Web browser, as shown in Figure 2.5.

FIGURE 2.5

Simple as it is, this

is an example of a

VBScript program.

This is a fine program, a good place to start, but I’m sure that you’re thinking right now that you want your ASP pages to do more than just display simple text.

I have good news for you. You can unleash all kinds of power and functionality and make your code run much more efficiently by utilizing some core VBScript statements. I want to give you a preview of two:

IF...THEN

FOR...NEXT

Many more statements are essential to your effective use of VBScript, but I will focus on these two statements in this introductory chapter.

The IF…THEN Statement

As I write this chapter, it is a very hot, humid summer afternoon. I live in Indiana, where we have the saying, “If you don’t like the weather in Indiana, wait five minutes and it will change!” Although this can make things interesting sometimes—for example, a beautiful summer morning can give rise to very dark clouds and the threat of a tornado—this also makes it difficult to plan for outdoor activity.

Imagine that you live in Indiana or someplace where the weather is very unpredictable. You are on a much needed vacation, and tomorrow you want to take your kids to the park for a day of outdoor fun. You want to plan for any contingency, so you decide that if it rains, you will take the gang to the movies instead. To express this as an IF...THEN statement, you could write it as IF it rains tomorrow, THEN I will take the kids to the movie.

Basically, when you use an IF...THEN statement, you want your code to test for a particular condition. If that condition is true (equal to some value or condition you’ve set), a certain set of actions will take place. In the vacation example, you can see that IF the weather is bad, THEN you will go to the movies. Going to the movies is the action that takes place if the condition you are evaluating, the weather, is true, in other words, if it rains.

The following code illustrates this analogy as a VBScript program:

Listing 2.6 Weather_Fun.asp

<html>

<body>

<%

Good_Weather="We are going to the park!"

Bad_Weather="We are going to the movies!"

WeatherCheck=2

IF WeatherCheck=2 THEN %>

The weather is bad -- <%=Bad_Weather%> <%

END IF

33

C h a p te r

2

P r o g r am m i n A g S P

W e P b a g e s

w i t h

V B S c ri p t

34

ASP Programming for the Absolute Beginner

%>

</body>

</html>

When this code is executed, it appears in the Web browser as shown in Figure 2.6. Go through this code to be sure that you understand everything that is happening:

1.After inserting the usual <body> and <html> tags, a <% is placed to indicate that everything that follows (up to the closing %> tag) should be evaluated as VBScript.

2.Two variables are set, Good_Weather and Bad_Weather. Recall from the earlier discussion that you are declaring constants by assigning permanent values to these two variables.

3.A third variable, WeatherCheck (a variable of the integer subtype, by the way) is set to 2. You will find that as you program, you assign various test variables so that depending on the value they are assigned, a specific condition will be true or false. In this example (and as you will see in a moment), a value of 2 for this variable indicates bad weather. A value of 1 could indicate good weather.

4.The IF...THEN statement comes into play. In this example, IF the WeatherCheck variable has been set to 2, THEN the value assigned to the Bad_Weather variable is displayed, via the <%=Bad_Weather%> statement.

5.Finally (as with many statements in VBScript), you need to indicate the end of the statement so that your program knows that it can move on to the next section of your code. This ending, for the IF...THEN statement, is indicated by the line END IF.

TRAP

Be careful that you don’t forget to place the closing statements (the END IF statement in the example). This is a very common oversight that results in program errors.

FIGURE 2.6

In this example, the

IF…THEN statement

results in some

stormy weather.

FIGURE 2.7

Your message of

bad weather

displays only if the

WeatherCheck

variable is set to 2.

Otherwise, you are

left with a blank

screen.

You might be wondering at this point what would happen if you set the WeatherCheck variable to something other than 2. Go ahead and try it. Set the WeatherCheck variable to 49 or any numeric value other than 2. Be sure to save your changes, and then load the program again into your Web browser. Your screen should look like Figure 2.7.

Having a blank screen display when a certain condition is not true is generally not good. Imagine if you were visiting this Web page and suddenly you were confronted with a blank screen. You would probably, and rightly, wonder whether something is wrong with the code, even though the code is working perfectly.

The code is working too perfectly, in fact. What you need at this point is a way to designate another action to occur if the WeatherCheck variable is set to something other than 2.

You do this with the IF...THEN...ELSE statement. Take a look at the following code listing:

Listing 2.7 More_Weather_Fun.asp

<html>

<body>

<%

Good_Weather="We are going to the park!"

Bad_Weather="We are going to the movies!"

WeatherCheck=2

IF WeatherCheck=3 THEN %>

The weather is bad -- <%=Bad_Weather%>

35

C h a p te r

2

P r o g r am m i n A g S P

W e P b a g e s

w i t h

V B S c ri p t