Table Of Content00 0672320681 FM 11/9/01 4:09 PM Page i
ASP.NET
Stephen Walther
Unleashed
00 0672320681 FM 11/9/01 4:09 PM Page ii
ASP.NET Unleashed EXECUTIVEEDITOR
Rosemarie Graham
Copyright ©2002 by Sams Publishing
MANAGINGEDITOR
All rights reserved. No part of this book shall be reproduced,stored in a
Charlotte Clapp
retrieval system,or transmitted by any means,electronic,mechanical,photo-
copying,recording,or otherwise,without written permission from the publish- PROJECTEDITOR
er. No patent liability is assumed with respect to the use of the information George E. Nedeff
contained herein. Although every precaution has been taken in the preparation
COPYEDITOR
of this book,the publisher and author assume no responsibility for errors or
Chuck Hutchinson
omissions. Nor is any liability assumed for damages resulting from the use of
the information contained herein. INDEXER
Larry Sweazy
International Standard Book Number:0-672-32068-1
Library of Congress Catalog Card Number:00-108313 PROOFREADER
Plan-it Publishing
Printed in the United States of America
First Printing:June 2001 TECHNICALEDITORS
J. Boyd Nolan
04 03 02 01 4 3 2 1
Donny Mack
Trademarks Doug Seven
All terms mentioned in this book that are known to be trademarks or service TEAMCOORDINATOR
marks have been appropriately capitalized. Sams Publishing cannot attest to Lynne Williams
the accuracy of this information. Use of a term in this book should not be
MEDIADEVELOPER
regarded as affecting the validity of any trademark or service mark.
Dan Scherf
Warning and Disclaimer
INTERIORDESIGNER
Every effort has been made to make this book as complete and as accurate as Gary Adair
possible,but no warranty or fitness is implied. The information provided is on COVERDESIGNER
an “as is”basis. The author and the publisher shall have neither liability nor
Aren Howell
responsibility to any person or entity with respect to any loss or damages aris-
ing from the information contained in this book or from the use of the CD or PAGELAYOUT
programs accompanying it. D&G Limited
00 0672320681 FM 11/9/01 4:09 PM Page iii
Contents at a Glance
Introduction 1
Part I Working with ASP.NET Web Forms 9
1 Building ASP.NET Pages 11
2 Building Forms with Web Server Controls 57
3 Performing Form Validation with Validation Controls 149
4 Advanced Control Programming 197
Part II Advanced ASP.NET Page Development 249
5 Creating Custom Controls with User Controls 251
6 Separating Code From Presentation 277
7 Targeting Mobile Devices with Mobile Controls 309
8 Using Third-Party Controls 359
Part III Working with ADO.NET 415
9 Introduction to ADO.NET 417
10 Binding Data to Web Controls 473
11 Using the DataListandDataGridControls 513
12 Working with DataSets 587
13 Working with XML 637
14 Using ADO.NET to Create a Search Page 667
Part IV Working with ASP.NET Applications 705
15 Creating ASP.NET Applications 707
16 Tracking User Sessions 747
17 Caching ASP.NET Applications 771
18 Application Tracing and Error Handling 811
Part V Securing ASP.NET Applications 859
19 Using Forms-Based Authentication 861
20 Using Windows-Based Authentication 911
21 Encrypting Data over the Network 927
00 0672320681 FM 11/9/01 4:09 PM Page iv
Part VI Building ASP.NET Web Services 955
22 Creating an XML Web Service 957
23 Advanced XML Web Services 995
Part VII Leveraging the .NET Framework 1043
24 Working with Collections and Strings 1045
25 Working with the File System 1099
26 Sending E-mail and Accessing the Network 1127
27 Creating Graphics On-the-Fly with GDI+ 1165
PART VIII Building Custom ASP.NET Controls 1213
28 Developing Custom Controls 1215
29 Advanced Control Development 1259
Part IX Sample ASP.NET Applications 1293
30 Creating a Job Site 1295
31 Creating an Online Store 1321
Part X Appendices 1343
A Migrating from ASP to ASP.NET 1345
B HTML Control Reference 1357
C Web Control Reference 1371
Index 1405
00 0672320681 FM 11/9/01 4:09 PM Page v
Table of Contents
Introduction 1
PART I Working with ASP.NET Web Forms 9
1 Building ASP.NET Pages 11
ASP.NET and the .NET Framework......................................................12
The .NET Framework Class Library................................................12
Understanding Namespaces..............................................................13
Standard ASP.NET Namespaces......................................................14
.NET Framework-Compatible Languages........................................15
Introducing ASP.NET Controls ............................................................17
A Simple ASP.NET Page ................................................................17
Benefits of ASP.NET Controls ........................................................22
Overview of ASP.NET Controls......................................................28
Adding Application Logic to an ASP.NET Page ..................................36
Handling Control Events..................................................................36
Handling Page Events......................................................................41
The Structure of an ASP.NET Page ......................................................45
Directives..........................................................................................45
Code Declaration Blocks..................................................................49
ASP.NET Controls............................................................................51
Code Render Blocks ........................................................................51
Server-side Comments......................................................................52
Server-side Include Directives..........................................................53
Literal Text and HTML Tags............................................................53
Summary................................................................................................55
2 Building Forms with Web Server Controls 57
Building Smart Forms............................................................................58
UsingLabelControls ......................................................................58
Using the TextBoxControl ..............................................................60
UsingButtonControls ....................................................................69
Using the RadioButtonandRadioButtonListControls..................78
Using the CheckBoxandCheckBoxListControls ............................96
Using the DropDownListControl....................................................110
Using the ListBoxControl ............................................................120
Controlling Page Navigation ..............................................................131
Submitting a Form to a Different Page..........................................132
Using the Redirect()Method ......................................................133
Working with the HyperLinkControl............................................135
00 0672320681 FM 11/9/01 4:09 PM Page vi
ASP.NET
vi
UNLEASHED
Applying Formatting to Controls........................................................136
Base Web Control Properties..........................................................136
Applying Styles to Web Controls ..................................................141
Summary..............................................................................................147
3 Performing Form Validation with Validation Controls 149
Using Client-side Validation................................................................150
Configuring Client-side Validation................................................151
Enabling and Disabling Client-side Validation..............................152
Requiring Fields:The RequiredFieldValidatorControl..................153
Comparing to an Initial Value........................................................157
Validating Expressions:The RegularExpressionValidator
Control..............................................................................................159
Validating E-Mail Addresses..........................................................161
Validating Usernames and Passwords............................................163
Validating Phone Numbers ............................................................165
Validating Web Addresses..............................................................166
Checking for Entry Length............................................................169
Validating ZIP Codes......................................................................170
Comparing Values:The CompareValidatorControl ..........................171
Comparing the Value of One Control to the Value
of Another....................................................................................173
Comparing the Value of a Control to a Fixed Value......................174
Performing a Data Type Check......................................................176
Checking for a Range of Values:The RangeValidator
Control..............................................................................................177
Summarizing Errors:The ValidationSummaryControl......................181
Displaying Pop-Up Error Messages ..............................................185
Performing Custom Validation:The CustomValidator
Control..............................................................................................188
Validating Credit Card Numbers....................................................192
Disabling Validation............................................................................194
Summary..............................................................................................195
4 Advanced Control Programming 197
Working with View State ....................................................................198
Disabling View State......................................................................199
Adding Values to View State..........................................................202
Displaying and Hiding Content ..........................................................203
Using the VisibleandEnabledProperties....................................203
Using the PanelControl ................................................................207
Simulating Multipage Forms..........................................................209
Adding Controls Programmatically................................................213
Adding Controls to a Page ............................................................216
00 0672320681 FM 11/9/01 4:09 PM Page vii
vii
CONTENTS
ThePlaceHolderControl ..............................................................218
Dynamically Generating Forms ....................................................219
Dynamically Generating List Items ..............................................222
Using Rich Controls............................................................................225
Displaying Interactive Calendars with the Calendar
Control ........................................................................................225
Rendering Calendar Days..............................................................233
Displaying Banner Advertisements with the AdRotator
Control ........................................................................................239
Accepting File Uploads with the HTMLInputFileControl ............244
Summary..............................................................................................247
PART II Advanced ASP.NET Page Development 249
5 Creating Custom Controls with User Controls 251
Including Standard Content with User Controls ................................252
Exposing Properties and Methods in User Controls ..........................257
Exposing Web Controls in User Controls............................................260
Exposing Events in User Controls ......................................................268
Loading User Controls Programmatically ..........................................269
Summary..............................................................................................275
6 Separating Code from Presentation 277
Creating Business Components ..........................................................278
Creating a Simple Business Component........................................279
Creating Multitiered Web Applications ..............................................290
Using Code-Behind..............................................................................296
How Code-Behind Really Works ..................................................299
Compiling Code-Behind Files........................................................300
Inheriting Multiple Pages from a Code-Behind File......................300
Compiling a Complete ASP.NET Page..........................................304
Summary..............................................................................................307
7 Targeting Mobile Devices with Mobile Controls 309
Using Mobile Device Software Simulators ........................................310
Introduction to the Wireless Application Protocol..............................312
Building WML Pages..........................................................................313
Configuring Internet Information Server ......................................313
WML and XML..............................................................................314
Creating a Deck of Cards ..............................................................314
Linking Files with WML................................................................316
Using ASP.NET Mobile Controls........................................................317
Creating Mobile Forms..................................................................318
Dynamically Activating a Mobile Form........................................320
Displaying Text..............................................................................321
Displaying Lists..............................................................................325
00 0672320681 FM 11/9/01 4:09 PM Page viii
ASP.NET
viii
UNLEASHED
Creating Text Boxes ......................................................................338
Validating User Input......................................................................341
Displaying Images..........................................................................344
Placing Phone Calls........................................................................346
Displaying Advertisements with Mobile Controls ........................347
Displaying Calendars with Mobile Controls..................................348
Creating Cross-Device-Compatible Mobile Pages..............................350
Detecting Mobile Capabilities........................................................351
Choosing Devices with DeviceSpecific ......................................353
Using Form Template Sets ............................................................356
Summary..............................................................................................357
8 Using Third-Party Controls 359
Using the Microsoft Internet Explorer WebControls..........................360
Using the TreeViewControl ..........................................................360
Using the ToolbarControl ............................................................376
Using the TabStripControl ..........................................................382
Using the Superexpert Controls..........................................................390
Using the Content RotatorControl..............................................390
Using the SuperDataGridControl..................................................398
Using the DataFormControl ..........................................................404
Summary..............................................................................................414
PART III Working with ADO.NET 415
9 Introduction to ADO.NET 417
An Overview of ADO.NET ................................................................418
Performing Common Database Tasks ................................................420
Opening a Database Connection....................................................421
Retrieving Records from a Database Table....................................423
Using Parameters with Queries......................................................431
Adding Records to a Database ......................................................437
Updating Database Records ..........................................................441
Deleting Database Records............................................................445
Improving Database Performance ......................................................449
Using SQL Stored Procedures........................................................449
Retrieving Return Values and Output Parameters..........................454
Executing Complex Stored Procedures..........................................457
Improving Performance with Connection Pooling........................461
Advanced Database Topics..................................................................462
Executing Database Commands in a Transaction..........................462
Specifying a Command Behavior..................................................466
Retrieving Table Schema Information............................................469
Summary..............................................................................................470
00 0672320681 FM 11/9/01 4:09 PM Page ix
ix
CONTENTS
10 Binding Data to Web Controls 473
Overview of Data Binding..................................................................474
Binding a Server Control to a Data Source ........................................481
Binding to the RepeaterControl....................................................481
Using Templates ............................................................................484
View State and the RepeaterControl............................................487
Binding to the DropDownListControl............................................487
Binding to the RadioButtonListControl......................................490
Binding to the CheckBoxListControl............................................492
Binding to a ListBoxControl........................................................494
Binding to Other Controls..............................................................496
Creating Master/Detail Forms ............................................................503
Creating Single-Page Master/Detail Forms....................................504
Creating Multipage Master/Detail Forms......................................507
Summary..............................................................................................511
11 Using the DataListandDataGridControls 513
Overview of the DataListandDataGridControls..............................514
Understanding Event Bubbling......................................................514
Using Templates ............................................................................516
Using the DataKeysCollection......................................................517
Using the DataListControl................................................................517
Displaying Data in a DataList......................................................517
Using Templates with a DataList..................................................520
Using Styles with a DataList........................................................522
Creating Multiple Columns in a DataList....................................525
Capturing Events Raised in a DataListControl............................527
Selecting Items in a DataListControl..........................................530
Using the DataKeysCollection with the DataListControl ..........533
Creating a Master/Detail Form with a DataListControl..............534
Editing Items in a DataListControl..............................................537
Using the DataGridControl................................................................545
Creating Columns in a DataGridControl......................................547
Using Styles with a DataGrid........................................................562
Sorting Columns in a DataGridControl........................................565
Paging Through Records in a DataGrid ........................................569
Selecting Rows in a DataGridControl..........................................575
Editing Items in a DataGridControl..............................................578
Editing Items in a DataGridControl with Templates....................581
Summary..............................................................................................585