+ All Categories
Home > Documents > Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James...

Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James...

Date post: 01-Oct-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
28
Matthew MacDonald and Mario Szpuszta, Revising Authors Pro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel Cazzulino Michael Clark Richard Conway Robert Eisenberg Brady Gaster James Greenwood Kevin Hoffman Erik Johansson Angelo Kastroulis Dan Kent Sitaraman Lakshminarayanan Don Lee Christopher Miller Matt Milner Jan Narkiewicz Matt Odhner Ryan O'Keefe Andrew Reid Matthew Reynolds Enrico Sabbadin Bill Sempf Doug Seven Srinivasa Sivakumar Thiru Thangarathinam Doug Thews
Transcript
Page 1: Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel

Matthew MacDonald and Mario Szpuszta,Revising Authors

Pro ASP.NET 2.0 in C# 2005

K. Scott AllenJames AveryRuss BasiuraMike BatongbacalMarco BellinasoMatt ButlerAndreas EideDaniel CazzulinoMichael ClarkRichard ConwayRobert Eisenberg

Brady GasterJames GreenwoodKevin HoffmanErik JohanssonAngelo KastroulisDan KentSitaraman LakshminarayananDon LeeChristopher MillerMatt MilnerJan Narkiewicz

Matt OdhnerRyan O'KeefeAndrew ReidMatthew ReynoldsEnrico SabbadinBill SempfDoug SevenSrinivasa SivakumarThiru ThangarathinamDoug Thews

Page 2: Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel

Pro ASP.NET 2.0 in C# 2005

Copyright © 2005 by Matthew MacDonald and Mario Szpuszta

All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,electronic or mechanical, including photocopying, recording, or by any information storage or retrievalsystem, without the prior written permission of the copyright owner and the publisher.

ISBN (pbk): 1-59059-496-7

Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1

Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrenceof a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademarkowner, with no intention of infringement of the trademark.

Lead Editor: Ewan BuckinghamTechnical Reviewers: Robert Lair, Jason LefebvreEditorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis,

Jason Gilmore, Jonathan Hassell, Chris Mills, Dominic Shakeshaft, Jim SumserAssociate Publisher: Grace WongProject Manager: Kylie JohnstonCopy Edit Manager: Nicole LeClercCopy Editor: Kim WimpsettAssistant Production Director: Kari Brooks-CoponyProduction Editor: Laura CheuCompositor: Dina QuanProofreaders: Liz Welch and Lori BringIndexer: Broccoli Information ManagementArtist: Kinetic Publishing Services, LLCInterior Designer: Diana Van WinkleCover Designer: Kurt KramesManufacturing Manager: Tom Debolski

Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], orvisit http://www.springeronline.com.

For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley,CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://www.apress.com.

The information in this book is distributed on an “as is” basis, without warranty. Although every precautionhas been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability toany person or entity with respect to any loss or damage caused or alleged to be caused directly or indi-rectly by the information contained in this work.

The source code for this book is available to readers at http://www.apress.com in the Source Code section.

Page 3: Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel

Contents at a Glance

About the Revising Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv

About the Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix

PART 1 ■ ■ ■ Core Concepts■CHAPTER 1 Introducing ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

■CHAPTER 2 Visual Studio 2005 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

■CHAPTER 3 Web Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

■CHAPTER 4 Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

■CHAPTER 5 ASP.NET Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

■CHAPTER 6 State Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195

PART 2 ■ ■ ■ Data Access■CHAPTER 7 ADO.NET Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229

■CHAPTER 8 Data Components and the DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265

■CHAPTER 9 Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295

■CHAPTER 10 Rich Data Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335

■CHAPTER 11 Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391

■CHAPTER 12 XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425

■CHAPTER 13 Files and Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471

PART 3 ■ ■ ■ Building ASP.NET Websites■CHAPTER 14 User Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505

■CHAPTER 15 Themes and Master Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525

■CHAPTER 16 Website Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555

■CHAPTER 17 Resources and Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599

■CHAPTER 18 Website Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619

iv

Page 4: Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel

PART 4 ■ ■ ■ Security■CHAPTER 19 The ASP.NET Security Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667

■CHAPTER 20 Forms Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689

■CHAPTER 21 Membership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709

■CHAPTER 22 Windows Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751

■CHAPTER 23 Authorization and Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777

■CHAPTER 24 Profiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803

■CHAPTER 25 Cryptography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839

■CHAPTER 26 Custom Membership Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 867

PART 5 ■ ■ ■ Advanced User Interface■CHAPTER 27 Custom Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 899

■CHAPTER 28 Design-Time Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 941

■CHAPTER 29 JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 975

■CHAPTER 30 Dynamic Graphics and GDI+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1011

■CHAPTER 31 Portals with Web Part Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1043

PART 6 ■ ■ ■ Web Services■CHAPTER 32 Creating Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1087

■CHAPTER 33 Web Service Standards and Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . 1129

■CHAPTER 34 Advanced Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1175

■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1211

v

Page 5: Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel

Contents

About the Revising Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv

About the Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix

PART 1 ■ ■ ■ Core Concepts■CHAPTER 1 Introducing ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

The Evolution of Web Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3The Development World Before ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3What’s Wrong with Classic ASP? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4ASP.NET 1.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

Seven Important Facts About ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7Fact 1: ASP.NET Is Integrated with the .NET Framework . . . . . . . . . . . . . . . . . . . . 7Fact 2: ASP.NET Is Compiled, Not Interpreted . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7Fact 3: ASP.NET Is Multilanguage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9Fact 4: ASP.NET Runs Inside the Common Language Runtime . . . . . . . . . . . . . . 11Fact 5: ASP.NET Is Object-Oriented . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12Fact 6: ASP.NET Is Multidevice and Multibrowser . . . . . . . . . . . . . . . . . . . . . . . . 14Fact 7: ASP.NET Is Easy to Deploy and Configure . . . . . . . . . . . . . . . . . . . . . . . . 14

ASP.NET 2.0: The Story Continues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15C# 2005 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16Visual Studio 2005 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16ASP.NET 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

■CHAPTER 2 Visual Studio 2005 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

The .NET Development Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24The Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24The Visual Studio IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

Websites in Visual Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26Projectless Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28Migrating a Visual Studio .NET Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29Designing a Web Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

The Visual Studio IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34Solution Explorer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36Document Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Toolbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

vii

Page 6: Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel

Error List and Task List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38Server Explorer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

The Code Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40Adding Assembly References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41IntelliSense and Outlining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

The Coding Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47How Code-Behind Files Are Connected to Pages . . . . . . . . . . . . . . . . . . . . . . . . 49How Control Tags Are Connected to Page Variables . . . . . . . . . . . . . . . . . . . . . . 50How Events Are Connected to Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . 50

Visual Studio Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52Single-Step Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52Advanced Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55Variable Watches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

Visual Studio Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57ASP.NET Development Helper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

■CHAPTER 3 Web Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

Page Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64HTML Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64Dynamic Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66The ASP.NET Event Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66Automatic Postbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67View State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69XHTML Compliance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

Web Forms Processing Stages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76Page Framework Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77User Code Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78Automatic Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79Cleanup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79A Page Flow Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80

The Page As a Control Container . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82Showing the Control Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82The Page Header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86Dynamic Control Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

The Page Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89Session, Application, and Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89Response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94Trace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95Accessing the HTTP Context in Another Class . . . . . . . . . . . . . . . . . . . . . . . . . . 100

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

■CONTENTSviii

Page 7: Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel

■CHAPTER 4 Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

Types of Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104The Server Control Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

HTML Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106The HtmlControl Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107The HtmlContainerControl Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108The HtmlInputControl Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108The HTML Server Control Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108Setting Style Attributes and Other Properties . . . . . . . . . . . . . . . . . . . . . . . . . . 110Programmatically Creating Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . 111Handling Server-Side Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113

Web Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116The WebControl Base Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117Basic Web Control Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119Enumerated Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121Focus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122The Default Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123Scrollable Panels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123Handling Web Control Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

The List Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127The Selectable List Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128The BulletedList Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130

Input Validation Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132The Validation Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133The Validation Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134The BaseValidator Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135The RequiredFieldValidator Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137The RangeValidator Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137The CompareValidator Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137The RegularExpressionValidator Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138The CustomValidator Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141The ValidationSummary Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142Using the Validators Programmatically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143Validation Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144

The ASP.NET Rich Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145The AdRotator Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146The Calendar Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150

■CHAPTER 5 ASP.NET Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

Anatomy of an ASP.NET Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152The Application Domain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152Application Lifetime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153

■CONTENTS ix

Page 8: Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel

Application Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154Application Directory Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154

The Global.asax Application File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155Application Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156Demonstrating Application Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158

ASP.NET Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159The Machine.config File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160The Web.config File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162Configuration Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164Reading and Writing Configuration Sections Programmatically . . . . . . . . . . . . . 168The Website Administration Tool (WAT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171Extending the Configuration File Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173Encrypting Configuration Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175

.NET Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177Creating a Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178Using a Component Through the App_Code Directory . . . . . . . . . . . . . . . . . . . . 179Using a Component Through the Bin Directory . . . . . . . . . . . . . . . . . . . . . . . . . 180

Extending the HTTP Pipeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182HTTP Handlers and HTTP Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183Creating a Custom HTTP Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184Configuring a Custom HTTP Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185Registering HTTP Handlers Without Configuring IIS . . . . . . . . . . . . . . . . . . . . . . 186Creating an Advanced HTTP Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187Creating a Custom HTTP Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193

■CHAPTER 6 State Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195

ASP.NET State Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196View State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198

A View State Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199Storing Objects in View State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200Retaining Member Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202Assessing View State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203Trimming View State in a List Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205View State Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206

Transferring Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207The Query String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207Cross-Page Posting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209Cross-Page Posting and Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211

Custom Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213Session State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214

Session Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214Using Session State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216Configuring Session State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217Securing Session State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222

■CONTENTSx

Page 9: Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel

Application State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223Static Application Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226

PART 2 ■ ■ ■ Data Access■CHAPTER 7 ADO.NET Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229

The ADO.NET Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230ADO.NET Data Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230Standardization in ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232SQL Server 2005 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233Fundamental ADO.NET Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233

The Connection Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234Connection Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235Testing a Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236Connection Pooling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237Connection Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239

The Command and DataReader Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240Command Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240The DataReader Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241The ExecuteReader() Method and the DataReader . . . . . . . . . . . . . . . . . . . . . . 242The ExecuteScalar() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247The ExecuteNonQuery() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247SQL Injection Attacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248Using Parameterized Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250Calling Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251

Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253Transactions and ASP.NET Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254Isolation Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258Savepoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259Nested Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260

Provider-Agnostic Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260Creating the Factory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260Create Objects with Factory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261A Query with Provider-Agnostic Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263

■CHAPTER 8 Data Components and the DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . 265

Building a Data Access Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265The Data Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267The Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267The Data Utility Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268Testing the Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274

■CONTENTS xi

Page 10: Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel

Disconnected Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276Web Applications and the DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277XML Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277

The DataSet Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278The DataTable Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279The DataRow Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280

The DataAdapter Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280Filling a DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280Working with Multiple Tables and Relationships . . . . . . . . . . . . . . . . . . . . . . . . 282Searching for Specific Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286Using the DataSet in a Custom Data Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287

The DataView Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288Sorting with a DataView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288Filtering with a DataView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290Advanced Filtering with Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292Calculated Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294

■CHAPTER 9 Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295

Basic Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296Single-Value Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296Other Types of Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298Repeated-Value Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302

Data Source Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309The Page Life Cycle with Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310

The SqlDataSource . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311Selecting Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312Parameterized Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314Handling Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318Updating Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318Disadvantages of the SqlDataSource . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322

The ObjectDataSource . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323Selecting Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324Updating Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328Updating with a Data Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329

The Limits of the Data Source Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331Adding the Extra Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332Handling the Extra Options with the SqlDataSource . . . . . . . . . . . . . . . . . . . . . 333Handling the Extra Options with the ObjectDataSource . . . . . . . . . . . . . . . . . . . 334

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334

■CONTENTSxii

Page 11: Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel

■CHAPTER 10 Rich Data Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335

The GridView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336Defining Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336

Formatting the GridView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339Formatting Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341Formatting-Specific Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344

GridView Row Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346Using Selection to Create a Master-Details Form . . . . . . . . . . . . . . . . . . . . . . . 346The SelectedIndexChanged Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348Using a Data Field As a Select Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349

Sorting the GridView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350Sorting with the SqlDataSource . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350Sorting with the ObjectDataSource . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351Sorting and Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353Advanced Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354

Paging the GridView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355Automatic Paging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355Custom Pagination with the ObjectDataSource . . . . . . . . . . . . . . . . . . . . . . . . . 357Customizing the Pager Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360

GridView Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361Using Multiple Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362Editing Templates in Visual Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363Binding to a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364Handling Events in a Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366Editing with a Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367

The DetailsView and FormView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372The DetailsView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372The FormView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374

Advanced Grids . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375Summaries in the GridView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375A Parent/Child View in a Single Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377Serving Images from a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379Detecting Concurrency Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389

■CHAPTER 11 Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391

Understanding ASP.NET Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392Output Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393

Declarative Output Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393Caching and the Query String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394Caching with Specific Query String Parameters . . . . . . . . . . . . . . . . . . . . . . . . 395Custom Caching Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396Caching with the HttpCachePolicy Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397Post-Cache Substitution and Fragment Caching . . . . . . . . . . . . . . . . . . . . . . . . 398

■CONTENTS xiii

Page 12: Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel

Cache Profiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400Caching to Disk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401

Data Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401Adding Items to the Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402A Simple Cache Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404Cache Priorities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405Caching with the Data Source Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406

Cache Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409File and Cache Item Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410Aggregate Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410The Item Removed Callback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411Understanding SQL Cache Notifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413Cache Notifications in SQL Server 2000 or SQL Server 7 . . . . . . . . . . . . . . . . . 415Cache Notifications in SQL Server 2005 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419

Custom Cache Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420A Basic Custom Cache Dependency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421A Custom Cache Dependency Using Message Queues . . . . . . . . . . . . . . . . . . . 422

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424

■CHAPTER 12 XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425

When Does Using XML Make Sense? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426An Introduction to XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426

The Advantages of XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427Well-Formed XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428XML Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429XML Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430

Writing and Reading XML Programmatically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431Writing XML Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432Reading XML Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435Validating XML Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447

Displaying XML Content with XSL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450A Basic Stylesheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450Using XslTransform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451Using the Xml Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452

XML Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453Nonhierarchical Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453Using XPath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455Nested Grids . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458Hierarchical Binding with the TreeView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459Using XSLT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461Binding to XML Content from Other Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . 463Updating XML Through the XmlDataSource . . . . . . . . . . . . . . . . . . . . . . . . . . . 464

XML and ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464Converting the DataSet to XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465Accessing a DataSet As XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466Executing an XML Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470

■CONTENTSxiv

Page 13: Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel

■CHAPTER 13 Files and Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471

Working with the File System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472The Directory and File Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472The DirectoryInfo and FileInfo Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474The DriveInfo Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477Working with Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478Filter Files with Wildcards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480Retrieving File Version Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480The Path Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481A File Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483

Reading and Writing Files with Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489Binary Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491Uploading Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492Making Files Safe for Multiple Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494Compression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498

Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502

PART 3 ■ ■ ■ Building ASP.NET Websites■CHAPTER 14 User Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505

User Control Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506Creating a Simple User Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506Converting a Page to a User Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508

Adding Code to a User Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508Handling Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508Adding Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510Using Custom Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512Adding Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514Exposing the Inner Web Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517

Dynamically Loading User Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518Portal Frameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519

Partial Page Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522VaryByControl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522Sharing Cached Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524

■CHAPTER 15 Themes and Master Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525

Standardizing Website Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525Cascading Style Sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525

Themes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528Theme Folders and Skins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529Applying a Simple Theme . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530

■CONTENTS xv

Page 14: Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel

Handling Theme Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531Creating Multiple Skins for the Same Control . . . . . . . . . . . . . . . . . . . . . . . . . . 532Skins with Templates and Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533Using CSS in a Theme . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535Applying Themes Through a Configuration File . . . . . . . . . . . . . . . . . . . . . . . . . 536Applying Themes Dynamically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536

Standardizing Website Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538Master Page Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538

A Simple Master Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539A Simple Content Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541Design-Time Quirks with Master Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543Default Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546A More Practical Master Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546Master Pages and Relative Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548Master Pages and Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549Applying Master Pages Through a Configuration File . . . . . . . . . . . . . . . . . . . . 549

Advanced Master Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549Specifying a Title and Metatags for a Content Page . . . . . . . . . . . . . . . . . . . . . 550Interacting with the Master Page Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550Dynamically Setting a Master Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552Nesting Master Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554

■CHAPTER 16 Website Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555

Pages with Multiple Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555The MultiView Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556The Wizard Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559

Site Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567Defining a Site Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568Binding to a Site Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569Breadcrumbs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570Binding Portions of a SiteMap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572Programmatic Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575Binding Other Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576Adding Custom Site Map Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577Creating a Custom SiteMapProvider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578URL Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583

The TreeView Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584The TreeNode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585Populating Nodes on Demand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587TreeView Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589

Menu Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592Menu Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595Menu Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598

■CONTENTSxvi

Page 15: Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel

■CHAPTER 17 Resources and Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599

Resources in .NET Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599Localization of Web Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607

Localization and the Common Language Runtime . . . . . . . . . . . . . . . . . . . . . . 607Local Resources for a Single Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611Sharing Resources Between Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615Localizing Static Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617Text Directions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618

■CHAPTER 18 Website Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619

Internet Information Services (IIS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619IIS and URL Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620Request Processing with IIS and ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622IIS 5.x Process Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623IIS 6.0 Process Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626Installing IIS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631

Managing Websites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634Creating a Virtual Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634Virtual Directories and Web Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 636Folder Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637

Managing Application Pools in IIS 6.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641Creating Application Pools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641Application Pools and Web Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644Custom Application Pool Identities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644

Deploying Your ASP.NET Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647Verifying the ASP.NET Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648ASP.NET Side-By-Side Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650Configure HTTP Runtime Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651Compilation Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652Deploying with Visual Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653

The VirtualPathProvider in ASP.NET 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655Health Monitoring in ASP.NET 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660

Understanding the Basic Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660Events and Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664

PART 4 ■ ■ ■ Security■CHAPTER 19 The ASP.NET Security Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667

What It Means to Create Secure Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667Understanding Potential Threats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668Secure Coding Guidelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668Understanding Gatekeepers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669

■CONTENTS xvii

Page 16: Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel

Understanding the Levels of Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 670Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 670Authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672Confidentiality and Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673Pulling It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673

Internet Information Services Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675IIS Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675IIS Authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676IIS and Secure Sockets Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677

ASP.NET Security Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 682Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 684Authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685The Security Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686Membership and Roles APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 688

■CHAPTER 20 Forms Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689

Introducing Forms Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689Why Use Forms Authentication? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 690Why Would You Not Use Forms Authentication? . . . . . . . . . . . . . . . . . . . . . . . . 692Why Not Implement Cookie Authentication Yourself? . . . . . . . . . . . . . . . . . . . . 693The Forms Authentication Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 694

Implementing Forms Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695Configuring Forms Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695Denying Access to Anonymous Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 698Creating a Custom Login Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 698Custom Credentials Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 704Persistent Cookies in Forms Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . 705

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707

■CHAPTER 21 Membership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709

Introducing the ASP.NET Membership API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709Using the Membership API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711

Configuring Forms Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713Creating the Data Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714Configuring Connection String and Membership Provider . . . . . . . . . . . . . . . . . 718Creating and Authenticating Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721

Using the Security Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723The Login Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724The LoginStatus Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733The LoginView Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733The PasswordRecovery Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735The ChangePassword Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739The CreateUserWizard Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740

Using the Membership Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744Retrieving Users from the Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745

■CONTENTSxviii

Page 17: Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel

Updating Users in the Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 747Creating and Deleting Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 748Validating Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749

■CHAPTER 22 Windows Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751

Introducing Windows Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751Why Use Windows Authentication? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751Why Would You Not Use Windows Authentication? . . . . . . . . . . . . . . . . . . . . . . 752Mechanisms for Windows Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753

Implementing Windows Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759Configuring IIS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759Configuring ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 760Denying Access to Anonymous Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 761Accessing Windows User Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 761

Impersonation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764Impersonation in Windows 2000 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 765Impersonation on Windows XP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766Impersonation and Delegation on Windows Server 2003 . . . . . . . . . . . . . . . . . 767Configured Impersonation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769Programmatic Impersonation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775

■CHAPTER 23 Authorization and Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777

URL Authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777Authorization Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778

File Authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 783Authorization Checks in Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 784

Using the IsInRole() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 784Using the PrincipalPermission Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785

Using the Roles Service for Role-Based Authorization . . . . . . . . . . . . . . . . . . . . . . . . . 787Using the LoginView Control with Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793Accessing Roles Programmatically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794Using the Roles Service with Windows Authentication . . . . . . . . . . . . . . . . . . . 796

Protecting Non-Web-Page Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 798Adding a File Type Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 798Writing a Custom HTTP Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 800

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 801

■CHAPTER 24 Profiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803

Understanding Profiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803Profile Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803How Profiles Store Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804Profiles and Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805Profiles vs. Custom Data Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 806

■CONTENTS xix

Page 18: Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel

Using the SqlProfileProvider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 806Creating the Profile Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 807Configuring the Provider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 809Defining Profile Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 810Using Profile Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 811Profile Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 812Profile Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 814Profiles and Custom Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 814The Profiles API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 818Anonymous Profiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 820

Building a Shopping Cart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 822The Shopping Cart Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 823The Test Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826Multiple Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 828

Custom Profiles Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829The Custom Profiles Provider Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829Designing the FactoredProfileProvider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 831Coding the FactoredProfileProvider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 832Testing the FactoredProfileProvider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 836

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 838

■CHAPTER 25 Cryptography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839

Encrypting Data: Confidentiality Matters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839The .NET Cryptography Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 840Understanding the .NET Cryptography Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843

Symmetric Encryption Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844Asymmetric Encryption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845The Abstract Encryption Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 846The ICryptoTransform Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 847The CryptoStream Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 847

Encrypting Sensitive Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 848Managing Secrets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 849Using Symmetric Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 850Using Asymmetric Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855Encrypting Sensitive Data in a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858

Encrypting the Query String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 861Wrapping the Query String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 862Creating a Test Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 864

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 866

■CHAPTER 26 Custom Membership Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 867

Architecture of Custom Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 867Basic Steps for Creating Custom Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 869

Overall Design of the Custom Provider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 869Designing and Implementing the Custom Store . . . . . . . . . . . . . . . . . . . . . . . . 870

■CONTENTSxx

Page 19: Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel

Implementing the Provider Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 876Using the Custom Provider Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 894

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 896

PART 5 ■ ■ ■ Advanced User Interface■CHAPTER 27 Custom Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 899

Custom Server Control Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 900Creating a Bare-Bones Custom Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 900Using a Custom Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 902Custom Controls in the Toolbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 903Creating a WebControl That Supports Style Properties . . . . . . . . . . . . . . . . . . . 904The Rendering Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 908

Dealing with Different Browsers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 909The HtmlTextWriter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 909Browser Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 910Browser Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 912Adaptive Rendering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 914

Control State and Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 915View State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 915Control State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 917Postback Data and Change Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 918Triggering a Postback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 921

Extending Existing Web Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 923Composite Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 923Derived Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 925

Templated Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930Creating a Templated Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930Using Customized Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 933Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 936

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 940

■CHAPTER 28 Design-Time Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 941

Design-Time Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 942The Properties Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 942Attributes and Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 945The Toolbox Icon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 946

Web Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 947Code Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 949

Type Converters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 949Serialization Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 957Type Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 962

Control Designers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 965A Basic Control Designer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 966

■CONTENTS xxi

Page 20: Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel

Smart Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 968The Action List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 969The DesignerActionItem Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 970The Control Designer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 972

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 973

■CHAPTER 29 JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 975

JavaScript Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 975JavaScript Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 976Script Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 978Rendering Script Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 986

Script Injection Attacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 987Request Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 988Disabling Request Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 988

Client Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 991Creating a Client Callback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 991Client Callbacks “Under the Hood” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 995

Custom Controls with JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 996Pop-Up Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 997Rollover Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1000Dynamic Panels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1003

Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1006Frame Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1007Inline Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1008

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1010

■CHAPTER 30 Dynamic Graphics and GDI+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1011

The ImageMap Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1011Creating Hotspots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1012Handling Hotspot Clicks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1013A Custom Hotspot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1014

Drawing with GDI+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1016Simple Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1017Image Format and Quality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1019The Graphics Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1020Using a GraphicsPath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1023Pens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1024Brushes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1026

Embedding Dynamic Graphics in a Web Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1028Using the PNG Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1029Passing Information to Dynamic Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1030Custom Controls That Use GDI+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1033

Charting with GDI+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1037Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1042

■CONTENTSxxii

Page 21: Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel

■CHAPTER 31 Portals with Web Part Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1043

Typical Portal Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1043Basic Web Part Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1045

Creating the Page Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1045WebPartManager and WebPartZones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1047Adding Web Parts to the Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1048Customizing the Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1051

Creating Web Parts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1054Simple Web Part Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1054Developing Advanced Web Parts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1062Web Part Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1070Connecting Web Parts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1076Authorizing Web Parts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1083Final Tasks for Personalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1084

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1084

PART 6 ■ ■ ■ Web Services■CHAPTER 32 Creating Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1087

Web Services Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1088The History of Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1088Distributed Computing and Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1089The Problems with Distributed Component Technologies . . . . . . . . . . . . . . . . 1091The Benefits of Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1091Making Money with Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1093The Web Service Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1093

Building a Basic Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1096The Web Service Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1096Web Service Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1097Exposing a Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1100Testing a Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1103

Consuming a Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1106The Proxy Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1112Creating an ASP.NET Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1113Creating a Windows Forms Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1115Creating an ASP Client with MSXML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1117Creating an ASP Client with the SOAP Toolkit . . . . . . . . . . . . . . . . . . . . . . . . . 1119

Refining a Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1120CacheDuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1120EnableSession . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1123BufferResponse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1126TransactionOption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1126

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1128

■CONTENTS xxiii

Page 22: Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel

■CHAPTER 33 Web Service Standards and Extensions . . . . . . . . . . . . . . . . . . . 1129

WS-Interoperability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1129SOAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1131

SOAP Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1132SOAP Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1133Tracing SOAP Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1134The SOAP Envelope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1136The SOAP Header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1140

WSDL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1144Viewing the WSDL for a Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1144The Basic Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1146Implementing an Existing Contract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1151

Customizing SOAP Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1152Serializing Complex Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1152Customizing XML Serialization with Attributes . . . . . . . . . . . . . . . . . . . . . . . . 1156Type Sharing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1159Customizing XML Serialization with IXmlSerializable . . . . . . . . . . . . . . . . . . . 1161Custom Serialization for Large Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . 1165Schema Importer Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1170

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1173

■CHAPTER 34 Advanced Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1175

Asynchronous Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1175Asynchronous Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1176A Simple Asynchronous Call . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1178Concurrent Asynchronous Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1180Responsive Windows Clients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1181Asynchronous Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1185

Securing Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1186Windows Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1186Custom Ticket-Based Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1189Tracking the User Identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1190Authenticating the User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1191Authorizing the User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1192Testing the SOAP Authentication System . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1192

SOAP Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1194Creating a SOAP Extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1196

The Web Services Enhancements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1203Installing the WSE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1204Performing Authentication with the WSE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1206

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1210

■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1211

■CONTENTSxxiv

Page 23: Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel

About the Revising Authors

■MATTHEW MACDONALD is an author, educator, and MCSD developer. He’s aregular contributor to programming journals and the author of more than adozen books about .NET programming, including ASP.NET: The CompleteReference (Osborne McGraw-Hill, 2002), Programming .NET Web Services(O’Reilly, 2002), Beginning ASP.NET in C (Apress, 2004), and Microsoft .NETDistributed Applications (Microsoft Press, 2003). In a dimly remembered pastlife, he studied English literature and theoretical physics.

■MARIO SZPUSZTA works in the Developer and Platform Group of MicrosoftAustria. Before he started working for Microsoft, Mario was involved in severalprojects based on COM+ and DCOM with Visual Basic and Visual C++ as wellas projects based on Java and J2SE. With beta 2 of the first version of the .NETFramework, he started developing web applications with ASP.NET. Currently,as a developer evangelist for Microsoft Austria, he conducts workshops, train-ings, and proof-of-concept projects with independent software vendors inAustria based on .NET web services and Office 2003 technologies.

xxv

Page 24: Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel

About the Technical Reviewers

■ROBERT LAIR is the president and CEO of Intensity Software (http://www.intensitysoftware.com),which specializes in Microsoft .NET consulting services. In addition to consulting services, Intensityoffers Kicks for .NET, a CICS-to-ASP.NET migration utility that automates the migration processwhile maintaining the existing business logic’s source code. Robert was one of the developers whocreated the original IBuySpy Store and Portal demo applications as well as the NetCOBOL for .NETversion of IBuySpy and the QuickStart samples. Robert has been a participating author for a numberof books and has written numerous articles about Microsoft .NET–related topics. Robert’s personalwebsite is at http://www.robertlair.com, and his blog is at http://www.robertlair.com/blogs/lair.

Robert would like to thank his beautiful wife, Debi, and four-year-old son, Max, for the familytime that was sacrificed while reviewing this book.

■JASON LEFEBVRE is the vice president and one of the founding partners of Intensity Software. Heuses Visual Studio and the Microsoft .NET Framework daily while architecting solutions for clientsof Intensity’s consulting services. He is also one of the developers who created the original IBuySpyStore demo application and its NetCOBOL for .NET translation. Jason has been a participatingauthor for a number of books and has written numerous articles about Microsoft .NET–relatedtopics.

He would like to thank his friends’ new puppy, Oliver, for being so cute.

xxvii

Page 25: Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel

Introduction

It’s not hard to get developers interested in ASP.NET. Without exaggeration, ASP.NET is the mostcomplete platform for web development that’s ever been put together. It far outclasses its predeces-sor, ASP, which was designed as a quick-and-dirty set of tools for inserting dynamic content intoordinary web pages. By contrast, ASP.NET is a full-blown platform for developing comprehensive,blisteringly fast web applications.

In this book, you’ll learn everything you need to master ASP.NET 2.0. If you’ve programmedwith a previous version of ASP.NET, you’ll sail through the basics and quickly begin learning aboutthe exciting new features in version 2.0. If you’ve never programmed with ASP.NET, you’ll find thatthis book provides a well-paced tour that leads through all the fundamentals, along with a back-stage pass that lets you see how the ASP.NET internals really work. The only requirement for thisbook is that you have a solid understanding of the C# language and the basics of .NET. If you’re aseasoned Java or C++ developer but you’re new to C#, you may find it easier to start with a bookabout .NET fundamentals before you read this one. Try C# and the .NET 2.0 Platform, Third Edition(Apress, 2005) for a comprehensive introduction or, for a quicker start, read A Programmer’s Intro-duction to C# 2.0, Third Edition (Apress, 2005).

ASP.NET from 1.0 to 2.0As you no doubt already know, ASP.NET is Microsoft’s next-generation technology for creatingserver-side web applications. It’s built on the Microsoft .NET Framework, which is a cluster ofclosely related new technologies that revolutionizes everything from database access to distributedapplications. ASP.NET is one of the most important components of the .NET Framework—it’s thepart that enables you to develop high-performance web applications and web services.

ASP.NET 1.0 was a revolution in the web programming world. It was so wildly popular that itwas licensed on thousands of commercial web servers through Microsoft’s Go-Live license programwhile it was still a beta product. ASP.NET 1.0 was finally released in early 2002.

ASP.NET 1.1 wasn’t as ambitious. Instead, it was just a chance for Microsoft architects to pauseand catch their collective breath. The focus in ASP.NET 1.1 wasn’t on new features—there weren’tany—but on performance tune-ups, security tweaks, and minor bug fixes. New features were quietlyshelved and saved for the next major milestone, ASP.NET 2.0. ASP.NET 1.1 was released late in 2003,solidifying ASP.NET as the web development platform of choice for professional developers.

Two long years later, ASP.NET 2.0 finally appeared on the horizon. Unlike the ASP.NET 1.0release, ASP.NET 2.0 doesn’t represent the start of a new direction in web development. In fact,almost all the underlying architecture that underpins ASP.NET 1.0 remains the same in ASP.NET 2.0.The difference is that ASP.NET 2.0 adds layers of higher-level features to the existing technology.Essentially, after the success of ASP.NET 1.0, Microsoft poured developers, time, and resourcesinto planning and preparing ASP.NET 2.0. Seeing as they no longer needed to rewrite the ASP.NETengine, the ASP.NET team members were free to be innovative with new controls, create better datamanagement solutions, build a role-based security framework, and even make a whole toolkit forcreating portal websites. In short, ASP.NET 2.0 gives developers a chance to relax and enjoy a wealthof new frills designed for their favorite platform. In this book, you’ll learn how to use, customize,and extend all these features.

xxix

Page 26: Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel

What Does This Book Cover?Here is a quick breakdown of what you’ll find in this book:

Part 1: Core Concepts: You’ll begin in Chapter 1 with a look at the overall ASP.NET platform,the .NET Framework, and the changes in store for ASP.NET 2.0. In Chapter 2 you’ll branch outto learn the tools of the trade—namely, Visual Studio 2005. In Chapters 3, 4, 5, and 6 you’ll learnthe key parts of the ASP.NET infrastructure, such as the web-page model, application configu-ration, state management, and caching. As you learn these core concepts, you’ll also take alow-level look at how ASP.NET processes requests and manages the lifetime of your web appli-cations. You’ll even learn how to extend the ASP.NET architecture.

Part 2: Data Access: This part tackles one of the core problem domains for all softwaredevelopment—accessing and manipulating data. In Chapters 7 and 8 you’ll consider the fun-damentals of ADO.NET as they apply to web applications and learn how to design data accesscomponents. In Chapter 9 and Chapter 10 you’ll learn about ASP.NET’s set of innovative data-bound controls that let you format and present data without writing pages of code. Chapter 11branches out into advanced caching strategies that ensure blistering performance. Finally,Chapters 12 and 13 move beyond the world of databases to show you how to work with XMLcontent and handle ordinary file access.

Part 3: Building ASP.NET Websites: In this part you’ll learn about essential techniques and fea-tures for managing groups of web pages. You’ll start simply with user controls in Chapter 14,which allow you to reuse segments of the user interface. In Chapter 15 you’ll consider twonew ASP.NET innovations—themes (for styling controls automatically) and master pages (forreusing a layout template across multiple pages). Chapter 16 shows how you can use the newnavigation model in ASP.NET 2.0 to let visitors surf from one page to another. Finally, Chapter 17explores localization, and Chapter 18 describes deployment and the IIS web server software.

Part 4: Security: In this part you’ll look at ASP.NET’s rich complement of security features.You’ll start with a high-level overview of security concepts in Chapter 19 and then learn the insand outs of forms authentication (Chapter 20) and the new Membership API that works withit (Chapter 21). In Chapter 22 you’ll tackle Windows authentication, and in Chapter 23 you’lllearn how to restrict authenticated users with sophisticated authorization rules and use role-based security. In Chapter 24 you’ll explore the Profiles API, a new, prebuilt solution for storinguser-specific information, and in Chapter 25 you’ll go one step further and learn how to protectthe data you store in a database as well as the information you send in a URL with encryption.Finally, Chapter 26 shows how you can plug into the ASP.NET security model by designing acustom Membership provider.

Part 5: Advanced User Interface: This part shows how you can extend web pages withadvanced techniques. In Chapter 27 and 28 you’ll tackle custom controls. In Chapter 29 andChapter 30 you’ll branch out to use JavaScript for dynamic pages and GDI+ for handcraftedgraphics. Finally, Chapter 31 explores the ASP.NET 2.0 Web Parts Framework for creating webportals.

Part 6: Web Services: Web services promise to revolutionize the way functionality is sharedacross different applications, network environments, and computing platforms. In Chapter 32you’ll start at the beginning; you’ll see how to create basic web services and how to use them inASP.NET web applications, .NET Windows applications, and even legacy ASP applications. InChapter 33 you’ll take a low-level look at the standards that make it all possible and see howthey work. In Chapter 34 you’ll learn how to use advanced techniques to call web services asyn-chronously, implement secure services, and start working with newer web service standardsusing the WSE (Web Services Enhancements) toolkit.

■INTRODUCTIONxxx

Page 27: Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel

Who Is This Book For?This book is intended as a primer for professional developers who have a reasonable knowledge ofserver-side web development. This book doesn’t provide an exhaustive look at every ingredient inthe .NET Framework—in fact, such a book would require twice as many pages. Instead, this bookaims to provide a lean, intelligent introduction to ASP.NET for professional programmers who don’twant to rehash the basics. Along the way, you’ll focus on other corners of the .NET Framework thatyou’ll need in order to build professional web applications, including data access and XML. Usingthese features, you’ll be able to create next-generation websites with the best tools on hand today.

This book is also relentlessly practical. You won’t just learn about features but you’ll also learnabout the real-world techniques that can take your website to the next level. Later chapters are dedi-cated to cutting-edge topics such as custom controls, dynamic graphics, advanced security, andhigh-performance data access, all with the goal of giving you everything you need to build profes-sional web applications.

To get the most from this book, you should be familiar with the syntax of the C# languageand with object-oriented concepts. You don’t need to have experience with a previous version ofASP.NET, as all the fundamentals are covered in this book. If you’re an experienced Java or C++developer with no .NET experience, you should consider supplementing this book with an intro-duction to .NET, such as A Programmer’s Introduction to C# 2.0, Third Edition (Apress, 2005).

What Do You Need to Use This Book?The main prerequisite for this book is a computer with Visual Studio 2005. Although you couldtheoretically write code by hand, the sheer tedium and the likelihood of error mean this approachis never used in a professional environment.

■Note You can use the scaled-down Visual Studio Web Developer 2005 Express Edition, but you’ll run into sig-nificant limitations on some of the examples. Most important, you can’t use Visual Studio Web Developer 2005Express Edition to create class libraries, which are an essential part of modern component-oriented design.

Additionally, to run ASP.NET pages, you need Windows 2000 Professional, Windows XPProfessional, Windows 2000 Server, or Windows Server 2003. You also need to install IIS (InternetInformation Services), the web hosting software that’s part of the Windows operating system, if youwant to create web services or test deployment strategies.

Finally, this book includes several examples that use sample databases that are included withSQL Server to demonstrate data access code, security techniques, and web services. If you use otherrelational database engines, the same concepts will apply, but you will need to modify the examplecode.

This book was created with the latest beta 2 and post–beta 2 builds of ASP.NET. BecauseASP.NET is just ending its beta cycle, it’s possible that the final release of the product will havesome changes. These changes may include new features or minor syntactic differences (such as arenamed property or method). To help manage the confusion, refer to the Source Code section ofhttp://www.apress.com to download the final release–ready code examples.

■INTRODUCTION xxxi

Page 28: Pro ASP.NET 2.0 in C# 2005978-1-4302-0064-2/1.pdfPro ASP.NET 2.0 in C# 2005 K. Scott Allen James Avery Russ Basiura Mike Batongbacal Marco Bellinaso Matt Butler Andreas Eide Daniel

Customer SupportWe always value hearing from our readers, and we want to know what you think about this book—what you liked, what you didn’t like, and what you think we can do better next time. You can sendyour comments by e-mail to [email protected]. Please be sure to mention the book title in yourmessage.

Sample CodeTo download the sample code, visit the Source Code section of the Apress site at http://www.apress.com, and select this book’s title. You can then download the sample code, which is com-pressed into a single ZIP file. Before you use the code, you’ll need to uncompress it using a utilitysuch as WinZip. Code is arranged into separate directories by chapter. Before using the code, refer tothe accompanying readme.txt file for information about other prerequisites and considerations.

ErrataWe’ve made every effort to make sure the text and the code contain no errors. However, no one isperfect, and mistakes do occur. If you find an error in the book, such as a spelling mistake or a faultypiece of code, we would be grateful to hear about it. By sending in errata, you may save anotherreader hours of frustration, and you’ll be helping us to provide higher-quality information. Simplye-mail the problem to [email protected], where your information will be checked and posted onthe errata page or used in subsequent editions of the book. You can view errata from the book’sdetail page.

■INTRODUCTIONxxxii


Recommended