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

ASP Programming for the Absolute Beginner

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

266 • Cleaner, more powerful, and faster code, with advanced DHTML sent to browsers capable of displaying such content, as well as regular HTML.

ASP Programming for the Absolute Beginner

Extensive controls for both data binding and form control, which provide tremendous precision in how data is displayed within the client. This enhanced integration of the information and the process in which it is displayed or manipulated gives the information a life of its own. It doesn’t just sit within your client; it communicates with the entire application structure.

Internet Information on ASP+

As of this writing, ASP+ and the entire Microsoft .NET are still very much in develop-

ment. However, many developers (of which you are now one, having read this book!)

are starting to look to ASP+ as their next tool for Web application development.

For more information, consult the following sources:

http://msdn.microsoft.com/msdnmag/issues/0900/ASPPlus/ASPPlus.aspY

http://www.devx.com/dotnet/resources/default.aspL

http://www-askasp-plus.com/articles.asp?View=FLL

http://www.asp101.com/aspplus

http://www.stardeveloper.com/asp bk aspplusM2.asp

http://4guysfromrolla.com/webtech/amb/amb.ASPPlus.shtmlA

Summary

E

T

 

Perhaps you are thinking that to stay on the cutting edge of ASP, you now need to run out and buy a book on ASP+. Perhaps you feel that all the material in this book is outdated and you would have been better off waiting for a book on ASP+. Think again. As with any new technology—especially one as sweeping in scope and vision as the

.NET initiative—early implementers must spend much time not on the cutting edge but the bleeding edge. In short, there is something to be said for allowing the technology to evolve and work out its kinks and bugs before you invest time and resources in utilizing it as your primary solution. Moreover, you always benefit by having a solid understanding of the fundamentals, which are often found in earlier versions. Then you appreciate what the new enhancements mean and can best benefit from all the functionality they represent. You did a smart thing in purchasing and learning from this book because you now have a foundation from which to move forward into the world of .NET and ASP+. The technologies you now understand (VBScript, SQL, the integration of databases with Web pages, and so on) are cornerstones of ASP, both current and future. Build from what you’ve learned, and continue to experience the power and fun of working with this ever-evolving technology.

Team-Fly®

AAP P E N D I X

VBScript Variable

Reference

Throughout this book, I’ve shown you how to tap the power of ASP using VBScript. This appendix serves as a

quick reference, a convenient place for you to look up everyday questions in regard to VBScript variable behavior.

268

ASP Programming for the Absolute Beginner

TRICK

You will discover, as you become more comfortable with ASP and VBScript, that

 

 

you won’t need to refer to a reference like this. Maybe you’ve already reached that

 

level (if you’re a quick learner!). For now, though, use this as a programming cheat

 

sheet while you master the essentials of the language.

VBScript and ASP Essentials

To get your VBScript to perform correctly within your ASP pages, you need to keep the following in mind:

Be sure that you save your VBScript-enabled Web pages with the .asp extension. This ensures that the VBScript included within your pages will execute on the server, sending only the results of that execution to the Web browser.

Aside from giving your pages the .asp extension, you must also ensure that your ASP Web pages are presented (and processed) via a Web server capable of working with ASP. The Microsoft Personal Web Server and Internet Information Server you’ve been using throughout this book are good examples of Web servers capable of working with ASP.

To integrate your VBScript into regular HTML, you must place the VBScript within <% and %> markings. For example, the following VBScript would properly execute in a Web page that has an .asp extension and is being processed by a Web server capable of working with ASP:

<html>

<body>

<%

TestVar="Active Server Pages"

%>

<b>Welcome to the exciting world of <%=TestVar%></b>

</body>

</html>

The following code would not work because the closing %> tag, after the definition of the TestVar variable, is not included. An error message would be displayed in the Web browser, attempting to access this page.

<html>

<body>

<%

TestVar="Active Server Pages"

<b>Welcome to the exciting world of <%=TestVar%></b>

</body>

</html>

I’ve shown you many coding examples utilizing these basic rules. Even though they are basic, they are essential. If you forget to follow these essentials of ASP programming with VBScript, your Web pages will, at best, not function and, at worst, present all kinds of nasty errors to the person visiting your site.

VBScript Variable Subtypes

Remember that a variable is a special programming component that can contain a variety of values, depending on how such values are set by the programming. For example, you could have a variable named Age to (temporarily) hold the age of each user as they are listed and read from a database.

Within VBScript are several variable subtypes, including the following:

Byte

Boolean

Integer

Long

Single

Double

Currency

Date

Object

String

Depending on the specific value you want to represent (for example, currency versus date), you can use these subtypes in different ways. I’ll show you how to work with several subtypes in this appendix.

TRICK

Understanding and working with variables is about as essential as it gets in learn-

 

 

ing a programming language. Also, be sure to refer to Chapter 2, “Programming ASP

 

Web Pages with VBScript,” for more information on this subject and for more de-

 

tailed examples of the information presented in this appendix.

Working with String Functions

The ability to manipulate and work with string functions is a major feature of VBScript. Within this section, I’ll show you how to

Concatenate strings.

Search and replace strings.

Compare one string to another.

Extract strings.

Concatenating Strings

If you concatenate a string, you are literally building a larger string from two or more smaller strings. Consider the following example:

<html>

<body>

269

A p p e n d i x

A

V B S c r ip V t a r ia b l e

R e fe r e n c e

270

ASP Programming for the Absolute Beginner

<%

ValueA="A good way to learn "

ValueB="to program in ASP is "

ValueC="to read ASP Programming for the Absolute Beginner"

ValueD=ValueA&ValueB&ValueC&ValueD

Response.Write(ValueD)

%>

</body>

</html>

When this ASP page executes, the text “A good way to learn to program in ASP is to read ASP Programming for the Absolute Beginner” is displayed. If you look closely at the code, you can see that three variables (ValueA, ValueB, and ValueC) have been assigned specific textual values. When combined together, using the & sign (and subsequently assigned to the ValueD variable), the entire concatenated value is displayed.

TRAP

Note that the & sign is used only to concatenate strings. If you want to add numeric variables, you can use regular numeric operators (+, -, /, and so on). I’ll talk more about numeric operators later in this appendix.

Searching and Replacing Strings

Often in your programming, you need to search for one string within another string. Fortunately, VBScript allows you to accomplish this task easily via the INSTR() function. By utilizing this function, you can determine the numeric position of one string within another string. Take a look at the following example:

<html>

<body>

<%=

INSTR("Now is the time for all good men to come to the aid of their

country", "good")%>

</body>

</html>

When this code executes, a numeric value of 25 is displayed on the screen. Why 25?

Because in evaluating the string "Now is the time for all good men to come to the aid of their

country", the INSTR function is looking for the position within the string that the word good begins. Starting at one and counting over the specific characters until the starting position of the word good, you will find that good starts at the twenty-fifth position. If you were searching for the starting position of the word time, the value returned would be 12 because time starts at the twelfth position in the string.

Note that you don’t have to start at the first position of a string. Consider the following code:

<html>

<body>

<%=

INSTR(4, "My name is John", "name")

%>

</body>

</html>

In this example, a value of 4 is returned. You are asking the INSTR function to search for the word name and to begin the search at the fourth position of the string "My name is John". Because the word name starts at the fourth position of the string, a value of 4 is returned. However, look at the following code:

<html>

<body>

<%=

INSTR(5, "My first name is John", "first")

%>

</body>

</html>

In this case, a value of 0 is returned. Why? Because even though the word first is in the fifth position of the string included within it (that is, the letter I), a value of 0 is returned because the search starts at the fifth position. A full match on the word first is not found because the f in first is not accounted for, given that the search begins at the fifth character and the letter f is at the fourth position.

You should also be aware that the INSTR function is, by default, case-sensitive. The following code returns the value of 0 for this reason:

<html>

<body>

<%=

INSTR("My first name is John", "FIRST")

%>

</body>

</html>

If the search string were listed as "My FIRST name is John", a value of 4 would be returned.

TRICK

You can get around this case-sensitive default by placing a 1 at the end of the func-

 

 

tion, as shown here:

<html>

<body>

<%=

INSTR(4, "My first name is John", "FIRST", 1)

%>

</body>

</html>

271

A p p e n d i x

A

V B S c r ip V t a r ia b l e

R e fe r e n c e

272

ASP Programming for the Absolute Beginner

TRICK

In this example, a value of 4 is returned because you are telling the INSTR function to

 

 

ignore its case-sensitive default. Be careful, though, when using this feature! You need

 

to include the start position argument (4,...), or your code will produce an error.

Finally, even though the INSTR function searches by default from the left of a string, you can get it to search from the right. Examine the following code example:

<html>

<body>

<%=

INSTRREV("My first name is John", "J")

%>

</body>

</html>

In this example, a value of 4 is returned because the search begins from the right of the string and searches until, in this case, a capital J is found (because you’ve left the default case-sensitive feature turned on), which happens to be the fourth letter from the right of the string.

Replacing strings comes in handy in a variety of situations. Consider the following code:

<html>

<body>

<%

User="Robert Smith"

NewString="We should tell you that username is a great ASP programmer!"

NewString=REPLACE(newstring, "username", User)

%>

<b>A special message!</b> <hr>

<%=NewString%>

</body>

</html>

When this code executes, the text “We should tell you that Robert Smith is a great ASP programmer!” appears on-screen under the boldface heading A special message!. As you can see in the code listing, the REPLACE function is used to tell your code what to look for (in this case, the word username) and replace it with the value assigned to the variable User.

Comparing One String to Another

At times, you want to compare two string values to see whether they are equal. Review the following code:

<html>

<body>

<%

VarA="My name is John"

VarB="My name is John"

IF VarA=VarB THEN %>

The two values are equal! <% ELSE %>

The two values are not equal! <% END IF%>

</body>

</html>

In this example, the text “The two values are equal!” is displayed because the two variables, VarA and VarB, are equal.

You should be aware, however, that the = operator is case-sensitive. To avoid returning false results when comparing strings (that is, returning a value indicating that the two strings being compared are not equal even though they are equal but have different capitalization), you can convert the strings to either uppercase or lowercase. Consider the following example:

<html>

<body>

<%

VarA="My name is John"

IF Ucase(VarA)="MY NAME IS JOHN" THEN %>

The two values are equal!

<% END IF %> </body> </html>

In this example, the variable VarA is converted to all uppercase, so the comparison is true.

TRICK

You can also convert to lowercase using the LCASE function in place of UCASE.

 

Finally, use the STRCOMP() function to compare two strings quickly, as shown in the following example:

<html>

<body>

<%=

STRCOMP("My name is John", "My name is John") STRCOMP("My NAME is John", "My name is John") STRCOMP("My NAME is John", "My name is John", 1) %>

</body>

</html>

273

A p p e n d i x

A

V B S c r ip V t a r ia b l e

R e fe r e n c e

274 What results are returned from this code listing? Take a look at each instance of the STRCOMP() function illustrated in the code:

ASP Programming for the Absolute Beginner

In the first example, a value of 1 (a true value) is returned because the two strings are identical.

In the second example, a value of 0 (a false value) is returned because the two strings are not equal in regard to case.

In the final example, a value of 1 (a true value) is returned. Even though the two strings are not identical in case, the inclusion of the ignore case argument (the 1 at the end of the third STRCOMP function) causes these two strings to compare as true because they are the same, with the exception of case.

Extracting Strings

Similar in scope to the search string function, the ability to extract string information is a powerful feature of VBScript and one you will use quite often.

In this section, you will take a look at the following functions:

LEFT()

RIGHT()

MID()

Put simply, the LEFT function returns a specified number of characters starting from the left side of a string. The RIGHT function returns a specified number of characters starting from the right side of a string. Finally, the MID function returns a specified number of characters starting from the left side of the string and continuing for a specified number. Examine the following code example:

<html>

<body>

<%

ExampleText="My name is John, and I work with ASP" %>

<%=LEFT(ExampleText, 7)%> <%=RIGHT(ExampleText, 13)%> <%=MID(ExampleText, 4, 4)%> </body>

</html>

If this code were to execute, the following values would be returned:

My name. This is representative of the LEFT function because it is returning the first seven characters (starting with one) and counting from the left.

work with ASP. This is representative of the RIGHT function because it is returning the first 13 characters (starting with one) and counting from the right.

name. This is representative of the MID function because it is returning a string of four characters, starting from the fourth position and counting from the left.

Replacing Strings

As you perform manipulations of your string functions, you will undoubtedly have a need to replace one value with another. This is where the REPLACE function comes in particularly handy:

<html>

<body>

<%=

TextValue="love"

NewTextValue="My name is John, and I like to program with ASP!"

NewTextValue=REPLACE(NewTextValue, "like", TextValue)

%>

<%=NewTextValue%>

</body>

</html>

Can you guess what happens when this executes? Take a look at each line of the code so that you can see how the REPLACE function works:

• First, the two variables TextValue and NewTextValue are assigned values ("love" and "My

name is John, and I like to program with ASP!").

Next, the REPLACE function is called into action. Because you want to replace some element of the NewTextValue string, you reassign this variable again

(NextTextValue=REPLACE(NewTextValue…).

The REPLACE function works like this: First, the variable you want to search through is listed (in this case, the initial value of the NewTextValue variable). Next, a value that you want to replace is listed (in this case, the word like). Finally, the variable or string (the variable TextValue, in this case) is listed. In essence, the REPLACE function works like this: You name the variable in which you want to replace something, you give a value you want to use to replace that something, and you list that something you want to replace.

TRICK

If you’re starting to see some similarities between these various functions, at least

 

 

in syntax, you are becoming a savvy ASP programmer. The syntax of many string

 

manipulation functions is very similar. This is by design, to give VBScript a high

 

degree of uniformity, usability, and functionality.

Working with Date and Time Functions

Knowing how to manipulate string variables is important—you will do lots of this in your work developing ASP. However, there are other common variable types you will find a need to work with just as often: date and time variables.

There are very simple (yet useful) time and date functions available for your use within VBScript:

DATE(). This function returns the current date.

TIME(). This function returns the current time.

275

A p p e n d i x

A

V B S c r ip V t a r ia b l e

R e fe r e n c e