Introducing Microsoft ® ASP.NET 2.0 Dino Esposito M A01T620245.fm Page 1 Thursday, June 10, 2004 2:51 PM PUBLISHED BY Microsoft Press A Division of Microsoft Corporation One Microsoft Way Redmond, Washington 98052-6399 Copyright © 2005 by Dino Esposito All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher. Library of Congress Cataloging-in-Publication Data Esposito, Dino, 1965- Introducing Microsoft ASP.NET 2.0 / Dino Esposito. p. cm. Includes index. ISBN 0-7356-2024-5 1. Active server pages. 2. Web sites Design. 3. Microsoft .NET. I. Title. TK5105.8885.A26E875 2004 005.2'76 dc22 2004044898 Printed and bound in the United States of America. 1 2 3 4 5 6 7 8 9 QWE 9 8 7 6 5 4 Distributed in Canada by H.B. Fenn and Company Ltd. A CIP catalogue record for this book is available from the British Library. Microsoft Press books are available through booksellers and distributors worldwide. For further information about international editions, contact your local Microsoft Corporation office or contact Microsoft Press International directly at fax (425) 936-7329. Visit our Web site at www.microsoft.com/learning/. Send comments to mspinput@microsoft.com. Active Directory, ActiveX, FrontPage, IntelliSense, Microsoft, Microsoft Press, MSDN, MSN, the .NET logo, Outlook, Verdana, Visual Basic, Visual Studio, the Visual Studio logo, Webdings, Win32, Windows, Windows NT, and Windows Server are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. Other product and company names mentioned herein may be the trademarks of their respective owners. The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious. No association with any real company, organization, product, domain name, e-mail address, logo, person, place, or event is intended or should be inferred. This book expresses the author’s views and opinions. The information contained in this book is provided without any express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its resellers or distributors will be held liable for any damages caused or alleged to be caused either directly or indirectly by this book. Acquisitions Editors: Danielle Bird Voeller and Ben Ryan Project Editor: Kathleen Atkins Copy Editor: Ina Chang Indexer: Lynn Armstrong Body Part No. X10-46133 ©Microsoft Press Introducing Microsoft® ASP.NET 2.0 A02L620245.p65 PP1 dwl LOCCORP To Silvia, Francesco, and Michela “I never let schooling interfere with my education.” —Mark Twain A03D620245.fm Page xi Wednesday, June 2, 2004 2:34 PM A03D620245.fm Page xii Wednesday, June 2, 2004 2:34 PM v Table of Contents Acknowledgments xi Introduction xiii Part I ASP.NET Essentials 1 Creating an ASP.NET 2.0 Application 3 Getting Started with Visual Studio 2005 4 Drawbacks of Visual Studio .NET 2003 4 Highlights of Visual Studio 2005 5 Creating a Sample Web Site 8 Designing Web Forms 10 Adding Code to Web Forms 11 The Local Web Server 12 Special Folders in ASP.NET 2.0 Applications 13 The Code-Beside Model 14 Evolving from Code-Behind 14 Compile-on-Demand for All Resources 15 Sharing Source Components 17 The Page Object Model 19 What’s New in the Page Class 20 Programming the Page 27 The Page Scripting Object Model 31 Overview of Server Controls 35 New Control Features 36 New Core Controls 38 Summary 43 2 Working with Master Pages 45 The Rationale Behind Master Pages 45 User Controls in ASP.NET 1.x 46 Include Files in Classic ASP 47 Outline of a Better Approach 48 A04T620245.fm Page v Thursday, June 10, 2004 12:04 PM vi Table of Contents What Are Master Pages? 50 Writing a Master Page 51 Writing a Content Page 55 Underpinnings of Master Pages 58 Merging Master and Content Pages 58 Nested Masters 60 Event Handling 64 A Realistic Example 66 Master Pages and Visual Inheritance 66 Layout of the Pages 68 Contents of the Pages 70 Programming the Master 72 Summary 75 3 Working with Web Parts 77 Building Pages with Web Parts 77 What Are Web Parts, Anyway? 78 Introducing the Web Parts Framework 79 A Sample Web Part Component 85 Editing and Listing Web Parts 94 Creating an Editor Zone 94 Adding Web Parts Dynamically 96 Connecting to Other Web Parts 99 The Connection Model 99 Building a Master/Detail Schema 101 Summary 106 4 Personalization: User Profiles and Themes 107 Managing User Profiles 108 Creating the User Profile 108 Personalization Providers 112 Interacting with the Page 116 Using Themes 122 What Are Themes? 122 Working with Themes 124 Summary 127 A04T620245.fm Page vi Thursday, June 10, 2004 12:04 PM Table of Contents vii Part II Data Access 5 What’s New in Data Access 131 Data-Binding Syntax Enhancements 132 The DataBinder Class 132 The XPathBinder Class 135 .NET Data Provider Enhancements 136 Connection String Storage 136 The Provider Factory Model 140 Batch Operations 144 SQL Server Provider Enhancements 145 Asynchronous Commands 146 Bulk Copy Operations 149 SQL Server 2005–Specific Enhancements 153 ADO.NET Class Enhancements 157 DataTable and DataSet Readers 157 Serializing a DataTable to XML 159 Managing Views of Data 162 Summary 164 6 The Data Source Object Model 165 The Rationale Behind Data Source Components 166 Codeless Data Binding 166 A Consistent Model for Heterogeneous Data Sources 170 A Richer Design-Time Experience 172 Data Source Control Internals 172 The IDataSource Interface 173 Tabular Data Source Controls 175 Hierarchical Data Source Controls 182 Using the SqlDataSource Control 187 Connecting to Data Source Controls 187 Data Source Parameters 188 Caching Behavior 191 Summary 193 A04T620245.fm Page vii Thursday, June 10, 2004 12:04 PM viii Table of Contents 7 Data-Bound Controls 195 Hierarchy of Data-Bound Controls 195 The DataBoundControl Base Class 198 Simple Data-Bound Controls 199 Composite Data-Bound Controls 202 Hierarchical Data-Bound Controls 205 The GridView Control 207 The GridView Object Model 208 The GridView Control in Action 214 Displaying Data 216 Editing Data 225 The DetailsView Control 230 The DetailsView Object Model 230 The DetailsView Control in Action 235 Creating Master/Detail Views 239 The FormView Control 241 The FormView Object Model 242 The FormView in Action 243 Summary 248 Part III Application Services 8 Rich Controls and Services 251 Creating Web Wizards 251 An Overview of the Wizard Control 252 Adding Steps to a Wizard 258 Navigating Through the Wizard 261 Generating Dynamic Images 264 The DynamicImage Control 265 Displaying Images in ASP.NET Pages 268 Advanced Site Functionality 273 Site Structure and Navigation 273 Site Counters 279 Summary 284 A04T620245.fm Page viii Thursday, June 10, 2004 12:04 PM Table of Contents ix 9 State Management 287 The Control State 288 Control State vs. View State 288 Programming the Control State 289 Extending the Session State Mechanism 291 The Default Session State Module 292 Customizing the Session State Module 294 Writing a Custom Session State Module 299 The ASP.NET Cache Object 300 The Cache Dependency Functionality 301 Designing a Custom Dependency 303 A Web Service–Based Cache Dependency 305 SQL Server Cache Dependency 309 Summary 313 10 ASP.NET Security 315 Using Forms Authentication 316 Forms Authentication Control Flow 317 Cookie-Based Forms Authentication in ASP.NET 1.x 317 A Cookieless Approach in ASP.NET 2.0 318 Configuring Forms Authentication 318 The FormsAuthentication Class 320 Managing Membership and Roles 326 The Membership Class 326 The Membership Provider 331 Managing Roles 336 Security-Related Controls 341 The Login Control 341 The LoginName Control 345 The LoginStatus Control 346 The LoginView Control 348 The PasswordRecovery Control 350 The ChangePassword Control 351 The CreateUserWizard Control 352 Summary 353 A04T620245.fm Page ix Thursday, June 10, 2004 12:04 PM x Table of Contents Part IV Advanced Topics 11 The ASP.NET Runtime Environment 357 Installing ASP.NET 2.0 358 Side-By-Side Backward Compatibility 358 Remapping Applications to ASP.NET 1.1 358 The ASP.NET Underpinnings 359 The IIS 5.x Process Model 360 The IIS 6.0 Process Model 361 The WebHost Application Management System 362 ASP.NET Runtime Components 366 Page Output Caching 373 The Structure of ASP.NET 2.0 Pages 377 A Sample Page 377 A Quick Look at the HTML Source Code 378 The Compilation Model 379 Dynamic Compilation 379 Site Precompilation 382 Summary 385 12 ASP.NET Configuration and Instrumentation 387 Changes to the Configuration API 388 Section-Specific Classes 388 Reading Configuration Settings 390 Writing Configuration Settings 391 The ASP.NET Administrative Tool 391 Changes to the Configuration Schema 392 Changed Configuration Sections 392 New Configuration Sections 393 The Health Monitoring API 394 Customizable Event-Level Analysis 395 Automated Notification of Problems 396 Summary 396 Index 399 A04T620245.fm Page x Thursday, June 10, 2004 12:04 PM [...]... refine the whole code-behind mechanism The ASP.NET 2.0 build system is also backward-compatible with the code-behind schema of older ASP.NET applications C01620245.fm Page 16 Wednesday, June 9, 2004 5:22 PM 16 Part I ASP.NET Essentials Note That ASP.NET 2.0 compiles class files (.cs and vb) on demand is a fact That ASP.NET 1.x doesn’t do the same is debatable ASP.NET 1.x does have the ability to compile... class All the members the class features in ASP.NET 1.x are supported in version 2.0 For a detailed explanation of ASP.NET 1.x members, please refer to my book Programming Microsoft ASP.NET (Microsoft Press, 2003) Infrastructure features such as theming and site counters will be covered in detail in Chapter 4 and Chapter 8, respectively The @Page Directive ASP.NET 2.0 adds some new attributes to the @Page... clear idea of the page life cycle in the ASP.NET runtime Overall, the structure of the page has not been revolutionized in the transition from ASP.NET 1.x to ASP.NET 2.0 However, quite a few new features have been added Some are related to the page as an object; some are inherited from the surrounding runtime environment What’s New in the Page Class In ASP.NET 2.0, a page supports some new features such... base class Page Unlike in ASP.NET 1.x, in ASP.NET 2.0 the page’s base class doesn’t change if code separation is used (In ASP.NET 1.x, C01620245.fm Page 20 Wednesday, June 9, 2004 5:22 PM 20 Part I ASP.NET Essentials when you use code-behind, the code-behind class becomes the parent of the dynamically generated page class.) The Page class is a built-in HTTP handler that the ASP.NET runtime invokes through... underlying technology— but the underlying idea was the same.) ASP.NET took five years to materialize—an entire geological era in Web development terms—finally arriving in 2002 It was the next step in the evolutionary process that started with ASP and found an excellent next version in JSP ASP.NET 2.0 is a major upgrade from there ASP.NET 2.0 features a new set of controls that simplify Web-based data... publicly accessible pages So what’s the role of Microsoft ASP.NET? ASP.NET provides an abstraction layer on top of HTTP with which developers build Web sites Thanks to ASP.NET, developers use high-level entities such as classes and components within the object-oriented paradigm Development tools assist developers during the work and make programming with the ASP.NET framework as seamless and quick as possible... special folders in the structure of an ASP.NET 2.0 application Table 1-1 Special Folders in ASP.NET Applications Folder Description Bin Contains all precompiled assemblies the application needs Code Contains source class files (.vb or cs) that the ASP.NET runtime engine dynamically compiles to assemblies Resources Contains resource files (.jpg, resx, xsd) that the ASP.NET run-time engine dynamically compiles... compilation step that ASP.NET needs to perform on all pages as required ASP.NET 2.0 has a new and improved compilation model that extends the compile-on-demand feature to class files and eliminates the need for Visual Studio NET to require IDE compilation for Web application projects Compile-on-Demand for All Resources ASP.NET 1.x supports the dynamic compilation of a few file types: ASP.NET pages (.aspx),... Studio stores any code in the class file specified through the CompileWith attribute Pages and projects do not have to be built to run Instead, ASP.NET compiles the page when it is first requested Partial Classes The compilation models of ASP.NET 1.x and ASP.NET 2.0 differ significantly because they are built on completely different underpinnings Pages that use code separation take advantage of a feature... 1 Creating an ASP.NET 2.0 Application 17 and code page into a single class that inherits the base Page class The class is then compiled into an assembly for execution Once you add a Web form with code separation, the top @Page directive in the page looks like this: . sites, .NET- 2- the-Max (http://www.dotnet2themax.com). A06I 6 20 245.fm Page xvi Wednesday, June 9, 20 04 4:54 PM Part I ASP. NET Essentials C01 6 20 245.fm Page 1 Wednesday, June 9, 20 04 5 :22 PM C01 6 20 245.fm. Twain A03D 6 20 245.fm Page xi Wednesday, June 2, 20 04 2: 34 PM A03D 6 20 245.fm Page xii Wednesday, June 2, 20 04 2: 34 PM v Table of Contents Acknowledgments xi Introduction xiii Part I ASP. NET Essentials 1. start with Visual Studio 20 05. C01 6 20 245.fm Page 3 Wednesday, June 9, 20 04 5 :22 PM 4 Part I ASP. NET Essentials Getting Started with Visual Studio 20 05 Visual Studio 20 05 is a container environment