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

ASP Programming for the Absolute Beginner

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

This page intentionally left blank

 

 

Y

 

L

 

F

 

M

 

A

 

E

 

T

 

 

Team-Fly®

Contents

 

Introduction ................................................................................

xix

C H A P T E R

 

 

 

Preparing to Work with ASP

1

 

Understanding ASP and the Web Server ....................................

2

1

The Advantages of Server-Side Processing ...........................

2

 

VBScript, the Heart of ASP .......................................................

3

 

Configuring Your Web Server for ASP ........................................

4

 

Configuring Internet Information Server

 

 

with Windows 2000 .......................................................................

4

 

Installing Internet Information Server .....................................

5

 

Confirming Your IIS Installation ...............................................

7

 

Configuring Personal Web Server with Windows 98 ...............

10

 

Installing Personal Web Server .............................................

10

 

Confirming Your PWS Installation .........................................

12

 

Confirming Your Web Server’s ASP Functionality ...................

13

 

Displaying Your Web Server’s Default Home Page ..............

15

 

Displaying the Default IIS Home Page ..............................

17

 

Displaying the Default PWS Home Page ..........................

18

 

Choosing an ASP Application Development Tool ....................

18

 

Summary .....................................................................................

20

C H A P T E R

 

 

 

Programming ASP Web Pages

 

 

with VBScript

23

2

Integrating VBScript with ASP ...................................................

24

 

Client-Side versus Server-Side Processing ..........................

27

 

Understanding VBScript Variables ............................................

29

 

Declaring Constants in VBScript ............................................

31

xii

Contents

 

Learning to Program with VBScript ...........................................

32

 

The IF...THEN Statement ........................................................

33

 

The FOR...NEXT Statement ....................................................

37

 

Commenting Your VBScript Code .............................................

39

 

The Hello, World! Game .............................................................

40

 

Summary .....................................................................................

41

C H A P T E R

 

 

 

Working with ASP Objects

43

 

Introducing the Request and Response Objects ......................

44

3

The Request Object .................................................................

45

 

Understanding Object Collections .............................................

47

 

Introducing the File Object .........................................................

52

 

The File Object Properties ......................................................

53

 

The File Object Methods ........................................................

54

 

A File Object Example ............................................................

54

 

Introducing the TextStream Object ............................................

56

 

Writing to a Text File ...............................................................

57

 

Reading from a Text File .........................................................

59

 

ASP MadLibs—Working with ASP Objects ...............................

61

 

The Game Data Input Form ....................................................

61

 

The MadLib Processing Page .................................................

63

 

The Save Feature: Storing Your ASP MadLibs

 

 

to a Text File ............................................................................

67

 

Summary .....................................................................................

69

C H A P T E R

 

 

 

Working with ASP Components 71

 

What Are ASP Components? .....................................................

72

4

Working with the Content Linking Component ........................

73

 

The Content Linking Component—An Example ..................

74

 

Working with the Ad Rotator Component .................................

76

 

The Ad Rotator Component—An Example ...........................

77

 

Working with the Content Rotator Component ........................

79

 

The Content Rotator Component—An Example ..................

80

 

Working with the Page Counter Component ............................

81

 

The Page Counter Component—An Example ......................

81

 

Summary .....................................................................................

81

C H A P T E R

 

 

 

Database Access with ADO

83

 

Understanding ADO ....................................................................

84

5

The ADO Object Collection ....................................................

85

 

Creating a Data Source Name (DSN) ........................................

86

 

Introducing SQL ..........................................................................

88

 

The SELECT Statement ..........................................................

92

 

The INSERT Statement ...........................................................

97

 

The UPDATE Statement .........................................................

99

 

The DELETE Statement ........................................................

102

 

The Counting Magician: Working with Recordsets ................

103

 

Summary ...................................................................................

105

C H A P T E R

 

 

 

Using Forms

109

 

The Memory Game ...................................................................

110

6

Form Essentials: The Response and Request Objects ...........

111

 

Reading and Manipulating Form Data ................................

113

 

The Memory Game: A Design Overview ................................

115

 

The Memory Database .............................................................

116

 

Establishing Database Permissions

 

 

with Windows 2000 ..............................................................

119

 

Programming the Memory Game ...........................................

120

 

Random Number Generation, Part I ....................................

120

 

Random Number Generation, Part II ...................................

124

 

Understanding the Entire Memory Game Code .....................

126

 

The Memory_Home.asp Page ..............................................

126

 

The Memory_Process.asp Page ...........................................

127

 

The Memory_Process.asp Page: Random Number

 

 

Generation ........................................................................

138

 

The Memory_Process.asp Page: Winning the Game ....

138

 

Summary ...................................................................................

141

xiii

C on t e n t s

xiv

Contents

C7H A P T E R

C8H A P T E R

Essential Programming Logic,

 

Part I

 

143

The Fortune Teller Game ..........................................................

144

Fortune Teller Basics .................................................................

 

145

Creating an ODBC Connection for the Fortune.mdb

 

Database ................................................................................

 

146

Creating the Fortune_Info.asp Data Input Page .................

148

Working with the If...Then Statement ......................................

149

Using the If...Then...

Else Statement ....................................

149

More Examples of If

...Then...Else ........................................

150

Working with Loops ..................................................................

 

151

Building the Fortune Teller Wizard ..........................................

154

Creating the Fortune_Process.asp Page .............................

154

Fortune_Process.asp—Defining the Page Header

 

Information ............................................................................

 

156

Fortune_Process.asp—Reading Previous Fortune Entries 156

Fortune_Process.asp—Generating the Magic Number

 

for the New Fortune

..............................................................

157

Fortune_Process.asp—Displaying a New Fortune .............

158

Summary ...................................................................................

 

159

Essential Programming Logic,

 

Part II

 

161

Introducing Arrays: Why Do You Need Them? .......................

162

Zero-Based Arrays ................................................................

 

164

Useful Array Functions .........................................................

165

UBOUND() .........................................................................

 

165

ERASE() .............................................................................

 

166

ISARRAY() .........................................................................

 

166

Useful VBScript Functions ...................................................

167

The Convert Functions .....................................................

167

Mathematical Functions ...................................................

168

The Is Functions ................................................................

 

169

Time and Date Functions .................................................

173

Programming the ASP War! Game .........................................

174

 

Analyzing the Code ...............................................................

179

 

Summary ...................................................................................

188

C H A P T E R

 

 

 

Formatting Processed Output

189

 

HTML Formatting—Beyond the Basics ...................................

190

9

Creating Dynamic Form Elements .......................................

194

 

Creating Dynamic Hyperlinks and QueryStrings ...............

197

 

Another Look—ASP MadLibs ...................................................

202

 

Improving ASP MadLibs!: Integrating with a Database ....

207

 

Improving ASP MadLibs!: Dynamic Page Formatting .......

209

 

Summary ...................................................................................

213

C H A P T E R

 

 

 

ASP and HTML Scripting with

 

 

FrontPage 2000

215

10Building a FrontPage Web ........................................................

216

 

Naming a FrontPage Web ....................................................

216

 

Adding New Pages to Your Web .........................................

218

 

FrontPage 2000 Basics ..........................................................

220

 

Creating Your First Web Page ..................................................

221

 

Placing and Formatting Text ................................................

221

 

Inserting Graphics .................................................................

223

 

Adding Tables to Organize Your Information .....................

226

 

Formatting Tables .................................................................

227

 

Adding Text and Graphics to Your Tables ...........................

230

 

Working with FrontPage Components ................................

231

 

Saving Your Web Pages .......................................................

234

 

Validating Forms .......................................................................

234

 

Determining Required Form Fields .....................................

235

 

Testing Form Validation in a Browser .............................

237

 

Validating Other Form Elements .........................................

239

 

Utilizing the FrontPage 2000 Database Results Wizard .........

240

 

Viewing the Automatically Generated ASP Code ..............

243

 

Integrating Forms with the Database Results Wizard ............

244

xv

C on t e n t s

xvi

Contents

 

Creating a Database Results Wizard Form

 

 

Processing Page ....................................................................

246

 

Working with Frames in FrontPage .........................................

249

 

Saving Frame Pages .............................................................

252

 

Understanding Frame Page Mechanics ..............................

253

 

Integrating E-mail with FrontPage Webs ................................

255

 

Working with DHTML Effects ...................................................

256

 

Summary ...................................................................................

258

C H A P T E R

 

 

 

Looking Back and

 

 

Looking Forward

261

11Configuring Your System for ASP ...........................................

262

 

Working with ASP on the Personal Web Server ................

262

 

Working with ASP on the Internet Information Server ......

262

 

Working with Other ASP-Related Tools ..............................

263

 

Programming ASP Pages with VBScript .................................

263

 

Integrating Your ASP Web Pages with a Database ................

264

 

Looking Ahead—ASP+ ..............................................................

264

 

The Philosophy of Indispensability—Viewing the Web

 

 

as an Automobile ..................................................................

264

 

The Promise of ASP+ ............................................................

265

 

Internet Information on ASP+ ..............................................

266

 

Summary ...................................................................................

266

A P P E N D I X

 

 

 

VBScript Variable Reference

267

 

VBScript and ASP Essentials ....................................................

268

A

VBScript Variable Subtypes ......................................................

269

 

Working with String Functions ................................................

269

 

Concatenating Strings ..........................................................

269

 

Searching and Replacing Strings ........................................

270

 

Comparing One String to Another ......................................

272

 

Extracting Strings .................................................................

274

 

Replacing Strings ..................................................................

275

 

Working with Date and Time Functions ..................................

275

ABP P E N D I X

SQL Reference

277

xvii

The SELECT Command ............................................................

278

C

The INSERT Command

278

on

t

 

 

The UPDATE Command

279

e

n

 

 

t

The DELETE Command

280

s

 

Logical Operators ......................................................................

280

 

The IS NULL Operator ..........................................................

280

 

The BETWEEN Operator .......................................................

281

 

The IN Operator ....................................................................

281

 

The LIKE Operator .................................................................

282

 

Useful Data-Sorting Functions .................................................

283

 

The COUNT Function ............................................................

283

 

The SUM Function ................................................................

283

 

The AVG Function .................................................................

284

 

The MAX Function ................................................................

284

 

The MIN Function .................................................................

284

 

A P P E N D I X

 

 

 

Access Essentials

285

 

Creating an Access Database ...................................................

286

C

Creating Tables in Design View................................................

287

 

Entering Data into a Table via the Datasheet View .................

292

 

Modifying a Table That Contains Data ....................................

293

 

Inserting a New Field into an Existing Table ......................

293

 

Deleting a Field from an Existing Table ..............................

295

 

Modifying a Field Data Type ................................................

296

 

Using the Access 2000 Simple Query Wizard .........................

297

Index

301

This page intentionally left blank

Introduction

Welcome to ASP Programming for the Absolute Beginner. By developing an interest in Active Server Pages (ASP), you stand at the threshold of a very exciting programming adventure. When first introduced, ASP offered the promise of an easy, powerful method of breaking through the functional

shackles of the Common Gateway Interface (CGI)—it has delivered on that promise. Now, several years into its history, ASP technology has become a central component of the Microsoft development strategy. Moreover, it has become the scripting language of choice for literally thousands of programmers, so you are in good company in wanting to learn more.

Although ASP is relatively easy to learn, it does require some degree of general programming knowledge to best utilize its power. Additionally, ASP allows for easy integration of data sources (Access, SQL Server, and so on), so an understanding of basic database development comes into play. Finally, because ASP is, at its core, a Web scripting language, you should have some basic knowledge of HTML to better understand how ASP can be used to bring a previously unleashed power and functionality to your Web pages.

This book does assume that you have some working knowledge of HTML. You’ve probably created a few Web pages, perhaps using a development tool such as Microsoft FrontPage or Macromedia HomeSite. Maybe you’ve also added some advanced functionality to said Web pages, perhaps by adding tables, Cascading Style Sheets (CSS), and forms. Maybe you’re familiar with another type of scripting language, such as JavaScript.

If you have done this level of Web development, you can build on this experience while learning to work with ASP. Even if you haven’t done much Web development (again, aside from having a general understanding of HTML), you can still learn to work with ASP—and have fun in the process. Don’t be intimidated by the programming aspect of ASP. Yes, you do have to learn some programming to most effectively work with ASP. However, I’m assuming from your purchase of this book that you are willing and ready to dive into the exciting and rewarding world of ASP development. This book shows you how to do just that.

Why Learn ASP, and What Can You Do with It?

If you’ve purchased this book or are thinking about purchasing it, you probably have some interest in advanced Web development. As already mentioned, you don’t need any prior programming experience to read and learn from this book. However, I am going to assume that although you might not know much about programming (yet),