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
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
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
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
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
xi
Acknowledgments
Introducing ASP.NET 2.0 was actually written twice. I started it in the summer
of 2003 and worked hard on it around the PDC 2003 timeframe, finishing up a
few hours before New Year’s Eve. But then it became clear that the public beta
program would not begin until summer 2004, so I had to rework it. The result
is a book written over four seasons. I hope you enjoy it as much as I enjoy four-
seasons pizza. (If you don’t have four-seasons pizza in your country, come to
cer games. However, I managed to have lunch at home almost every day (when
I was in Rome), to sleep at least six hours every night (when I was at home),
and to eat enough vegetables and fruits (to be a good example to my kids). I
also took the kids to school every day at 8:30 (except when I was traveling in
the States). Does this mean that I’ll be voted MVP? (And I don’t mean that nice
award that Microsoft grants to valuable professionals—I’m interested in another
kind of award—for Most Valuable Parent.) Kids, please vote for your dad!
A05A620245.fm Page xii Thursday, June 3, 2004 4:58 PM
xiii
Introduction
What’s a Web application? Basically, it’s a set of publicly accessible pages
bound to a well-known URL. No matter which direction Web-related technolo-
gies take in the future, this basic fact will never change. The reason is the
underlying transport protocol, HTTP. If we were to change the underlying pro-
tocol, we would end up with a different type of application. Period.
For all practical purposes, Web development began 10 years ago. Since then,
we’ve seen numerous technologies emerge, from short-lived ones such as
Microsoft ActiveX documents to watershed technologies such as Microsoft Active
Server Pages (ASP). The arrival of ASP in 1997 made it clear that real-world Web
development would be possible only through a rich and powerful server-side pro-
gramming model.
Much as Microsoft Visual Basic did for Windows development, ASP pro-
vided a set of server tools for building dynamic Web applications quickly and
effectively. More important, it pointed the way ahead. ASP wasn’t perfect (or,
more accurately, not yet perfected), so vendors improved the model by adding
object orientation and dynamic code compilation. Java Server Pages (JSP) intro-
duced key concepts such as compilation, components, tag customization, and a
first-class programming language. (This was a different company, different plat-
form, different programming paradigm, and different underlying technology—
but the underlying idea was the same.)
■ One of the following Microsoft Windows versions with Microsoft
Internet Information Services (IIS) installed:
❑ Windows 2000
❑ Windows XP Professional
❑ Windows Server 2003
■ Visual Studio 2005 (Beta 1 or the March Community Tech Preview
[build 2.0.40301])
■ Microsoft SQL Server 2000
The book doesn’t specifically require a beta version of SQL Server 2005
(code-named Yukon).
Notice that most examples that use SQL Server assume a blank sa pass-
word, although the use of a blank password is strongly discouraged in any seri-
ous development environment. If you don’t use a blank sa password in your
SQL Server installation, you must add your own password to the connection
strings or add the ASP.NET user to the login of the Northwind database. In the
latter case, you can use TRUSTED_CONNECTION=true in the connection strings
in place of the sa user and the blank password.
A06I620245.fm Page xiv Wednesday, June 9, 2004 4:54 PM
Introduction xv
Code Samples
This book doesn’t have a companion CD; all of the code samples are available
on the Web at http://www.micr osoft.com/learning/books/pr oducts/6962. Click
the Companion Content link in the More Information box on the right side of
the page.
The language used in the book is C#, and sample code is available only in
C#. All of the examples are wrapped up in a single Visual Studio 2005 applica-
tion and can be easily run from a central console, as shown here:
FIDR01
Support
Every effort has been made to ensure the accuracy of this book and the com-
such as classes and components within the object-oriented paradigm. Develop-
ment tools assist developers during the work and make programming with the
ASP.NET framework as seamless and quick as possible. Development tools are
ultimately responsible for the application being created and deployed to users.
They offer a programming paradigm and force developers to play by the rules
of that paradigm.
The key development tool for building ASP.NET 2.0 applications is
Microsoft Visual Studio 2005—the successor to Visual Studio .NET 2003. It has a
lot of new features and goodies expressly designed for Web developers to over-
come some of the limitations that surfaced from using Visual Studio .NET 2003.
In this chapter, we’ll cover the three basic elements of an ASP.NET appli-
cation—the IDE you use to build it, the page, and the core controls that make
it run. We’ll start with Visual Studio 2005.
C01620245.fm Page 3 Wednesday, June 9, 2004 5:22 PM
4 Part I ASP.NET Essentials
Getting Started with Visual Studio 2005
Visual Studio 2005 is a container environment that integrates the functionality of
multiple visual designers. You have a designer for building Windows Forms
applications, one for building ASP.NET sites, one for building Web services, and
so on. Visual Studio .NET 2003 has a single model for designing applications: the
project-based approach. Real-world experience has shown that this is not the best
approach—at least as far as ASP.NET and Web applications are concerned.
Drawbacks of Visual Studio .NET 2003
Visual Studio .NET 2003 designs applications around the concept of the project,
which is the logical entity that originates any application—be it Windows
Forms, Web, console, or Web service. Developers build an application by cre-
ating a new project, configuring it, and then adding pages, Web services,
classes, and controls. In terms of implementation, the project is an XML file that
links together some other files and directories. As far as Web applications are
concerned, a Visual Studio .NET project requires a Microsoft Internet Informa-
development is the inability to open a single page outside of a project. You can
open and edit a page, but IntelliSense won’t work; the same happens with
other key features, such as running and debugging the page. Frankly, in this
type of scenario, Visual Studio .NET offers only one advantage over Notepad—
HTML syntax coloring.
Highlights of Visual Studio 2005
Visual Studio 2005 provides a simpler and friendlier way to create Web Forms
applications. The key improvements address all the shortcomings detailed ear-
lier. Let’s review these features briefly.
Visual Studio 2005 supports multiple ways to open Web sites. In addition
to using FrontPage Server Extensions, you can access your source files using
FTP or a direct file system path. You can also directly access the local installa-
tion of IIS, browse the existing hierarchy of virtual directories, and access exist-
ing virtual roots or create new ones. IIS is not a strict requirement for the
development tool to work. Like Web Matrix, Visual Studio 2005 ships with a
local Web server that makes IIS optional, at least for testing and debugging
purposes. Figure 1-1 shows the user interface of the embedded Web server.
F01DR01
Figure 1-1 The local Web server in action in Visual Studio 2005
C01620245.fm Page 5 Wednesday, June 9, 2004 5:22 PM
6 Part I ASP.NET Essentials
The local Web server is a revised version of Cassini, the free mini–Web
server that originally shipped with Web Matrix. The local Web server is the
default option unless you explicitly open the project from an existing IIS virtual
directory. As Figure 1-2 demonstrates, you can open your Web site using a file
system path or an IIS virtual directory. In the former case, the local Web server
is used to test the site.
F01DR02
Figure 1-2 The ASP.NET application is controlled by the local Web
server if the Web site is opened from a file system path.
Web site by choosing File | New and choosing Web Site. The dialog box that
appears prompts you for the type of site you want to create. You’ll notice a cou-
ple of similar-looking options—ASP.NET Web site and ASP.NET Internet site, as
shown in Figure 1-5.
F01DR05
Figure 1-5 The options available to create a new Web site
with Visual Studio 2005
If you select the Web Site option, Visual Studio generates the minimum
number of files for a Web site to build. Basically, it creates a default.aspx page
and an empty Data directory. If you opt for an Internet site, an ASP.NET starter
kit is used to give you a functional Web site with several standard features built
in. Let’s go for a Web site.
Important Visual Studio 2005 creates a project file but doesn’t use it
to track all the files that form an application. The root directory of the
site implicitly defines a Web project. To add a new file to the project,
you just copy or create that file to the directory, and it is in the project.
If it isn’t, right-click in Solution Explorer and click Refresh Folder.
To edit a Web page, you can choose from three views—Design, Source,
and Server Code. The Design view displays the HTML layout, lets you select
and edit controls and static elements, and provides a graphical preview of the
C01620245.fm Page 8 Wednesday, June 9, 2004 5:22 PM
Chapter 1 Creating an ASP.NET 2.0 Application 9
page. The Source view shows the HTML markup along with the inline code.
The markup is syntax-colored and enriched by features such as IntelliSense,
tips, and autocompletion. The Server Code view shows only the inline code, if
any. The good news is that Visual Studio also applies syntax coloring to the
inline code.
You choose the template of the item to add to the site from the menu
shown in Figure 1-6.
F01DR06