Summary of Contents
Introduction 1
Chapter 1: Introducing ASP 5
Chapter 2: Decision Making, Loops, and Arrays 35
Chapter 3: Functions 67
Chapter 4: Objects, Properties, Methods, and Events 97
Chapter 5: Error Handling 127
Chapter 6: Extending Dreamweaver MX 145
Chapter 7: Advanced ASP Databases 169
Chapter 8: File Handling 193
Chapter 9: Using XML with ASP 219
Chapter 10: Sessions and Cookies 243
Chapter 11: Integrating Flash MX and ASP 261
Case Study 1 : A Simple Slogging System 291
Case Study 2: Message Forum 317
Index 357
Dreamweaver MX: Advanced ASP Web Development
Edward Apostol
Omar Elbaga
Dan Short
Rob Turnbull
© 2003 Apress Originally published by glasshaus in 2003
ISBN 978-1-59059-195-6 ISBN 978-1-4302-5498-0 (eBook) DOI 10.1007/978-1-4302-5498-0
Dreamweaver MX: Advanced ASP Web Development
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except
in the case of brief quotations embodied in critical articles or reviews.
The authors and publisher have made every effort in the preparation of this book to ensure the accuracy of the information. However, the information contained in this book is sold without warranty,
either express or implied. Neither the authors, glasshaus nor its dealers or distributors will be held liable for any damages caused or alleged to be caused either directly or indirectly by this book.
~shaus © 2003 Apress
Originally published by glasshaus in 2003
Trademark Acknowledgements glasshaus has endeavored to provide trademark information about all the companies and products
mentioned in this book by the appropriate use of capitals. However, glasshaus cannot guarantee the accuracy of this information.
Authors Edward Apostol Omar Elbaga
Dan Short Rob Turnbull
Technical Reviewers Kapil Apshankar Bryan Ashcraft Midhun James
Allan Kent Drew McLellan
Murray Summers
Proof Reader Agnes Wiggers
Indexer Bill Johncocks
Commissioning Editor Simon Mackie
Brand Visionary Bruce Lawson
Credits
Cover Image
Lead Technical Editor Chris Mills
Technical Editors Alessandro Ansa
Matt Machell Mark Waterhouse
Publisher Viv Emery
Project Managers Helen Cuthill
Sophie Edwards
Graphic Editors Rachel Taylor Pip Wonson
Production Assistants Paul Grove
Tina Ramwell
Cover Dawn Chellingworth
The cover image of this book was created by Don Synstelien of http://www.synfonts.com, co-author of the glasshaus book Usability: The Site Speaks For Itself. You can find more of Dan's illustration work
online at http://www.synstelien.com.
About the Authors
Edward Apostol
Edward Apostol is a man of many hats, playing the role of Internet Consultant, Developer, and Instructor in E-Commerce, Wireless, and New Media Studies. Edward has been a cornerstone in the Toronto web design community, being one of the first instructors to be involved with and teach Internet development in Canada since 1997.
Edward has been involved in many projects, including work for Compaq Corporation and Lotus Corporation. His work has been featured in many industry magazines such as Create Online, and web sites such as Macromedia.com. Edward has also spoken at numerous conferences in North America on a wide variety of web development topics. Edward's recent accomplishments include sharing a prestigious London International Advertising Award for best in New Media (self-promotional category), for the site http://www.shawnsfolkart.com.
His thoughts, reflections and web experiments can be found at http://www.edapostol.com. Edward is currently also associated with onX Enterprise Solutions (http://www.onx.com ) as well as Centennial College (http://www.bccc.com) in Toronto.
I would like to acknowledge and thank first and foremost the love of my life, Marilyn, for supporting me through my crazy work lifestyle; my parents and my brother Michael, for just "being there"; Chris Mills and the folks of glasshaus Publishing, who make this effort possible; and thanks to my many current and former students, as well as my colleagues and friends, who help push me to new limits of technical innovation.
Omar Elbaga
Starting out as a fine artist, Omar Elbaga gradually moved to computer graphic arts. He was particularly amazed by the power of the World Wide Web, so he embarked upon building small-scale sites for fun utilizing HTML and his art background. Falling in love with designing web pages and its potential, he began a career in web design. Omar has since been in the web development field for several years. With his head in computer books nearly 24 hours a day, Omar moved on to enhance his skills from web design to web programming.
Most of his work involves building database-driven web sites for small companies. Omar is currently a Tutorial Manager for udzone.com and runs a popular Dreamweaver MX resource site named udnewbie.com.
Having had the opportunity to study the Arabic language abroad for several months in an intensive course at the Fajr Center in Cairo, Omar is also a translator who translates mostly 12th century scholarly Arabic manuscripts into English. Omar is currently a senior at New Jersey City University completing a double major in English Literature and Secondary Education.
Dan Short
Daniel Short never planned to be a Web designer, it just happened. He started out in the Army tearing apart computers and eventually began putting together web sites. Dan is a devoted Macromedian (and Team Macromedia Volunteer) and uses almost the entire Macromedia Web Design Suite, including Fireworks and Macromedia Flash. He's been doing the Web gig since the end of 1998, and has had great luck building his web design business through Web Shorts Site Design. Dan helps to maintain several HTML and Dreamweaver reference sites including dwfaq.com, for which he created the style changer and all ASP functionality, including the Snippets Exchange and the DWfaq/Store.
He's also written articles for several resource sites, including AListApart.com and the Macromedia Designer and Developer Center, http://www.macromedia.com/desdev.
He is also a contributing author to the Dreamweaver MX Bible from Hungry Minds and Dreamweaver MX Magic from New Riders.
Rob Turnbull Rob Turnbull is the senior developer for Lighthouse; an established new media Design Company based in Shrewsbury, UK. Clients across Europe from small businesses to blue chip companies provide an increasing workload, which includes the development of databases, web sites, multimedia presentations, interactive CO-ROMs, promotional videos and 30 artwork in both animated and still form.
His personal Web site, http://www.robgt.com, is primarily focused on offering help and guidance to fellow Dreamweaver and UltraDev users including tutorials and links to helpful resources and some useful extensions.
My thanks go to the. invisible team working just as hard behind the scenes that actually get a book like this published; you guys have been great to work with. Perhaps more importantly, my thanks, my gratitude and my respect go to Sarah. Your care and support have been fantastic and /look forward to seeing you in the daylight hours again!
Introduction 1
What's It All About? .......................................................................................... 1
Who's This Book for? ........................................................................................ 1
What Do I Need to Begin? ................................................................................ 2
What's Inside? .................................................................................................. 2
Support and Feedback ...................................................................................... 3
Web Support ......... .. ....................................................................................................... 3
Chapter 1: Introducing ASP 5
Go Dynamic with ASP ...................................................................................... 6
Familiarize Yourself with ASP Basics .............................................................. 7
ASP Delimiters <o/o ... 0/o> ....................... ................ ................................. ........ ... .... ....... ?
Response.Write .................................................................... ........................... ....... ... .. . 9
The <0/o= Shortcut ...................................................................................................... 1 0
Commenting Code ...................................................................................................... 11
VBScript Basics .............................................................................................. 14
Working with Variables .................................................................................... 14
So Why Dim Variables? .............................................................................................. 15
Outputting Variables .................................................................................................... 15
Variable Scope? .......................................................................................................... 16
Data Types ........................................................................................................ 17
lnteger .......................................................................................................................... 17
String .......................................................................................................................... 18
Boolean ........................................................................................................................ 20
An Aside: Finding Out a Variable's Data Type ............................................................ 21
Date ............................................................................................................................ 21
Empty and Null ............................................................................................................ 22
Naming Your Variables ................................................................................................ 23
VBScri pt Operators ........................................................................................ 25
Comparison Operators ................................................................................................ 25
Logical Operators ........................................................................................................ 26
Arithmetic Operators .................................................................................................... 28
Operator Precedence ...................................................................................... 31
Summary .......................................................................................................... 33
Chapte on Making, d Arrays 35
Decision-Making Process .............................................................................. 35
If ... Then ... Else, Else If ............................................................................................ 36
Select Case ................................................................................................................ 39
Looping Through Code .................................................................................. 41
While ... Wend ............................................................................................................ 41
Do While/Until ... Loop ................................................................................................ 43
ii
For ... Next .................................................................................................................. 45
For Each ... Next ....... ...... ... ..... ................................................................................... 46
The Dreaded Infinite Loop ............................ .... ........ ................................... .............. .47
Working with Arrays ........................................................................................ 48
Anatomy of an Array ... ...... ................. .......................................................................... 48
Looping Through an Array .......................................................................................... 52
Converting a Recordset to an Array .......... ................................... ..................... .... ... ... 55
Putting It All Together .................................................................................... 55
Getting the Records .............. ...................................................................................... 56
Building the Array and Gathering Stats ...................................................................... 58
Send the Emails ......... ................................. ................................................................ 61
Summary .......................................................................................................... 65
Chapter 3: Functions 67
What Are They? .............................................................................................. 67
Built-In Functions ............................................................................................ 72
String Functions .......................................................................................................... 72
Typecasting Functions ................. .............. .... ............................................................. 73
Math Functions ............................................................................ ................................ 73
Date Functions ......... ... ........................... .... ................................ ...................... ........... 74
Function Power ................................................................................................ 74
String Functions ................................................ .. ........................................................ 74
Typecasting Functions ................................ ......... ........ ............. .................................. 81
Math Functions ............................................. ........................ ..... ............ ... .. ...... ....... .. .. 85
Date Functions .. ........ .. ........ .............................. ......... ..... ........... ................................. 87
Summary .......................................................................................................... 94
iii
Objects ............................................................................................................ 98
Methods & Properties .................................................................................................. 98
Collections .................................................................................................................. 98
Events .......................................................................................................................... 99
Instances .................................................................................................................... 99
Building Objects .......................................................................................................... 99
global.asa ...................................................................................................... 1 02
ASP Built-in Objects .................................................................................... 1 03
Application Object ...................................................................................................... 1 03
Response Object ...................................................................................................... 1 08
Request Object .......................................................................................................... 114
Server Object ............................................................................................................ 121
Summary ........................................................................................................ 124
Common Coding Practices .......................................................................... 128
Trapping Errors .............................................................................................. 131
The Err Object .......................................................................................................... 132
Turning Error Handling Off Again .............................................................................. 136
The ASP Error Object ................................................................................................ 136
Summary ........................................................................................................ 143
Anatomy of an Extension ............................................................................ 146
The Extension Code .................................................................................................. 146
The lnterface .............................................................................................................. 147
Behind the Scenes .................................................................................................... 153
iv
Preparing for Distribution ............................................................................ 156
Creating the MXI file .. .......... ............ ............ ...... ..................... .. ....... .... ......... .. ........... 156
Creating the MXP File ................................ ... ..... .... .................... .............................. 160
The Second Server Behavior ...................................................................... 161
Installing Extensions .................................................................................... 163
The Extension Manager ... .... ..... .... ......... ....... ............................................................ 164
Extension Resources .................................................................................... 165
Personal Favorites ........... .......... .................................... ........................................... 166
Summary ........................................................................................................ 166
Chapter 7: Advanced ASP Databases 169
Stored Procedures ........................................................................................ 170
Using Parameters ............. ............................ ........................ .. ................................... 171
The IF Statement ... .............. .............. ........... ............................... ..... ......... ... ... ......... 172
Triggers .......................................................................................................... 173
The UPDATE Trigger ..... .... .......................................................... ............................. 173
Sample Database .......................................................................................... 17 4
The Tables ........... ......................... .. .......................................................................... 176
The Relationships ........... ............... .............. .............................................................. 177
The View .......... ............. ............. ............ ..... ... .......... ............ ... .... ........ .......... ............. 179
The Stored Procedures ........ ..... ........................ .............................. ......................... 179
Dreamweaver MX Command Object ............................................................ 181
Order History .............................................. ...................................................... ........ 182
Unfulfilled Orders ............ ............... ........ ..... ..... ................. ... ...... ........ ....................... 186
Cancel an Order ................................................... ...................... .. ............................. 188
Backing Up Your Database .......................................................................... 190
Summary ........................................................................................................ 191
v
Objects and Collections .............................................................................. 193
Server.MapPath .............................................................................................. 195
Dynamic Image Gallery ................................................................................ 196
Playing with Folders and Files .................................................................................. 197
Reading Text Files .................................................................................................... 206
Writing a Text File ...................................................................................................... 21 0
Uploading Files .............................................................................................. 214
Encoding .................................................................................................................... 215
Upload Components .................................................................................................. 216
Pure ASP Solutions .................................................................................................. 217
Summary ........................................................................................................ 217
<defined>XML<Idefined> .............................................................................. 220
Working with XML Files ................................................................................ 222
Building a Tag Library ................................................................................................ 223
XML and Templates .................................................................................................. 227
XSLT .......................................................................................................................... 230
Transforming XML ........................................................................................ 233
Transforming Local Files ............................................................................................ 233
XML for Data Portability ............................................................................................ 235
Transforming Remote Files ........................................................................................ 235
Things to Watch Out For. ........................................................................................... 240
Summary ........................................................................................................ 240
vi
Chapter 10: Sessions and Cookies 243
Objects .......................................................................................................... 243
The Session Object ...................... ....... ... ......................................... ......................... 244
Application Variables .................................................................................... 245
Active Users .............................................................................................................. 246
Session Variables .......................................................................................... 247
Creating Session Variables ......................... ............................... ................................ 247
Removing Session Variables .... ........... ...................... ............................................... 248
Killing the Session ........ ............................................................................................ 248
Session Variable Server Behaviors ............................................................ 249
Create Session Variable ............................ ..................... ... ..... ... ...... ............. .. .... ....... 249
Remove Session Variable ............................. ...................................................... ....... 251
Abandon the Session ................................................................................................ 251
Cookies .......................................................................................................... 252
Creating Cookies ..... ................................... .............................................................. 252
Removing Cookies .......................................... ........... .......... ..................................... 254
Cookie Server Behaviors .............................................................................. 255
Get All Data Sources Server Behavior ........................................................ 258
Summary ........................................................................................................ 259
Chapter 11: Integrating Flash MX and ASP 261
A Brief History of Flash ................................................................................ 261
Using Flash by Itself ........................................... ................. ... ....... ... .... ...................... 262
Using Flash with ASP ...... .................... .... .... .................................... .......................... 263
Initiated by Flash ................................................................................................ ...... 263
Initiated by ASP (Back to Flash ... the Return Trip) ......... .... ...... ... ... .. ........... .. ............ 266
vii
Flash MX in the Dreamweaver MX Environment ........................................ 267
Inserting Flash .......................................................................................................... 267
Inserting Flash MX Buttons and Animated Flash MX Text ........................................ 269
Creating a Simple Application with Flash MX & ASP via Dreamweaver MX .. 269
A Brief Note About XML versus Databases .............................................................. 269
Creating a User Interface Prototype in XHTML ........................................................ 269
Recreating the Form in Flash MX ............................................................................ 271
Creating the ASP to Handle the Form Data .............................................................. 279
Return to Flash .......................................................................................................... 283
Adding More Functionality .......................................................................... 285
Summary ........................................................................................................ 288
Case 1: A eBio , Syst 291
The Database ................................................................................................ 292
A Basic Site Map .......................................................................................... 293
The Cascading Stylesheet ............................................................................ 294
The Admin Pages .......................................................................................... 296
The Admin Login Page .............................................................................................. 296
The Admin Main Page .............................................................................................. 302
The Admin Add Content Page .................................................................................. 304
The Admin Edit Content Page .................................................................................. 307
Summing Up the Admin Pages ................................................................................ 308
The Display Pages ........................................................................................ 308
The Homepage .......................................................................................................... 308
The Month Page ........................................................................................................ 312
The Day Page ............................................................................................................ 313
Summing Up the Display Pages ................................................................................ 314
viii
The Next Level .............................................................................................. 314
Room for Improvement ........................................................... ..... .. ............................ 315
Additional Functionality ........ ... ..... ... .......... ................................. ......... .... .... ..... .......... 315
Summary ........................................................................................................ 315
Case Study 2: Message Forum 317
lntroduction .................................................................................................... 317
How the Forum Will Work ............................................................................ 317
Specific Forums: Subject Matter ........ ..... ..... ... .. ....... ........ ..... ..... ... ............................. 318
Forums Need Topics .................................................................................................. 318
Topics Need Replies .................................................................................................. 318
Don't Forget About the Users ......................................................................... ........... 318
How Does the Forum Flow? ........................................................................ 318
Displaying Text from a Database .............................................................................. 319
Inserting Text into a Database .................................................................................. 319
Editing Text in Database ............................................................................................ 319
Relating Posts to Users ............................................................................................ 319
The Foundation, the Database .................................................................... 320
The forums Table ................................................................................................... ... 320
The forum_topics Table ................... ......................................................................... 321
The forum_replies Table .............. ... .................... ...... ................................................. 321
The users Table .................................. ............... .. ...... ..... ............ ... ............. .............. 322
Database Table Relationships .................................................................................. 322
Populating the Database ......................................... ................................................. 324
The Connection ............................................................................................ 325
ix
The Pages ...................................................................................................... 325
empty. asp .................................................................................................................. 325
login.asp .................................................................................................................... 326
logout. asp .................................................................................................................. 328
register. asp ................................................................................................................ 328
forums.asp ................................................................................................................ 329
topics.asp .................................................................................................................. 331
post_topic.asp ............................................................................................................ 334
Pause: Test the Application .......................................................................... 336
replies.asp ................................................................................................................ 337
post_reply.asp ............................................................................................................ 341
Preparing for edit_topic.asp ...................................................................................... 343
edit_reply.asp ............................................................................................................ 348
delete_posts.asp ........................................................................................................ 349
Showing the Number of Posts .................................................................... 350
Summary ........................................................................................................ 354
X
xi