CORE INTERNET
APPLICATION
DEVELOPMENT
WITH ASP.NET 2.0
This page intentionally left blank
Randy Connolly
CORE INTERNET
APPLICATION
DEVELOPMENT
WITH
ASP.NET 2.0
Upper Saddle River, NJ • Boston • Indianapolis • San Francisco
New York • Toronto • Montreal • London • Munich • Paris • Madrid
Capetown • Sydney • Tokyo • Singapore • Mexico City
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and the publisher was aware of a trademark
claim, the designations have been printed with initial capital letters or in all capitals.
The author and publisher have taken care in the preparation of this book, but make no expressed or implied
warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for
incidental or consequential damages in connection with or arising out of the use of the information or
programs contained herein.
The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special
sales, which may include electronic versions and/or custom covers and content particular to your business,
training goals, marketing focus, and branding interests. For more information, please contact:
U.S. Corporate and Government Sales
(800) 382-3419
[email protected]
For sales outside the United States, please contact:
International Sales
[email protected]
Contents
Preface xxi
Acknowledgments xxvii
About the Author xxix
PART I
CORE ASP.NET 1
Chapter 1
INTRODUCING ASP.NET 2.0 3
Why ASP.NET? 3
Static Versus Dynamic Web Content 4
Competing Dynamic Server Technologies 5
ASP.NET Advantages 8
.NET Framework 8
Components of .NET Framework 10
.NET Execution 14
ASP.NET Web Forms 15
C# Language 21
Web Application Structure 21
Contents
viii
Visual Studio 2005 24
Visual Studio Web Projects 25
Web Server Options 26
Tutorial: Creating ASP.NET Web Forms 29
Creating a Web Site in Visual Studio 30
Adding a New Web Form 31
Adding HTML Content to a Web Form 33
Adding Programming Logic 39
Encountering Errors 43
Using the Visual Studio Debugger 46
Introducing Server Controls 98
HTML Server Controls 98
Web Server Controls 98
Validation Controls 99
User Controls 99
Custom Server Controls 99
Overview of Web Server Controls 99
Common Members 101
Manipulating Properties Programmatically 104
The Essential Standard Web Server Controls 107
Label Control 108
Literal Control 110
TextBox Control 112
Button-Style Controls 115
CheckBox Control 123
RadioButton Control 126
List-Style Controls 127
Image Control 138
ImageMap Control 141
HyperLink Control 145
HiddenField Control 147
Table Control 149
Calendar Control 158
Summary 177
Exercises 177
Key Concepts 177
References 178
Chapter 4
THE ADDITIONAL STANDARD
WEB SERVER CONTROLS 179
VALIDATION CONTROLS 257
Error Handling 257
.NET Exception Handling 258
Exception Handling at the Class Level
Using a try…catch Block 259
Exception Handling at the Page Level 262
Exception Handling at the Application Level 264
Using the Validation Server Controls 271
ASP.NET Form Validation Process 273
Contents
xi
RequiredFieldValidator Control 280
ValidationSummary Control 282
CompareValidator Control 284
RangeValidator Control 287
RegularExpressionValidator Control 288
CustomValidator Control 296
Validation Groups 302
Summary 309
Exercises 309
Key Concepts 309
References 310
Chapter 6
CUSTOMIZING AND MANAGING YOUR
SITE’S APPEARANCE 311
Changing the Appearance of Server Controls 311
Using Common Appearance Properties 312
Using CSS with Controls 314
Appearance Properties, CSS, and ASP.NET 319
Using Themes and Skins 320
Consuming the XML Site Map 378
Programming the Site Map 383
Using Different Site Maps 385
Other Features of the Site Map 388
SiteMapPath Control 388
Styling the SiteMapPath 390
Integrating Query Strings into the SiteMapPath 393
Menu Control 397
Using the Menu Control 401
Changing the Appearance of the Menu 406
Handling Menu Events 414
TreeView Control 421
Understanding the TreeView Control 423
Using the TreeView Control 426
Changing the Appearance of the TreeView 427
Using Other Data with the TreeView Control 431
Responding to TreeView Events 435
Summary 442
Exercises 442
Key Concepts 443
References 444
Contents
xiii
PART II
WORKING WITH DATA 445
Chapter 8
DATA BINDING AND REPRESENTATION 447
Introducing Data Binding 448
How to Use Data Binding 448
What Can Be a Data Source? 449
Programming a DbConnection 511
Storing Connection Strings 513
Connection Pooling 515
DbCommand Classes 515
Creating a DbCommand 517
SQL Commands for Retrieving, Adding,
Updating, or Deleting Data 517
Stored Procedures 519
Executing a DbCommand 520
Using DbParameters 521
Using Transactions 525
DbDataReader Classes 529
Programming a DbDataReader 530
Implicit Connection Closing 532
Tutorial: Reading and Updating Data 534
DbDataAdapter Classes 544
Filling a DataSet 545
Updating Data 547
Data Provider-Independent ADO.NET Coding 550
Data Source Controls 554
Using Parameters 557
Modifying Data 560
How Do Data Source Controls Work? 561
Using the ObjectDataSource 562
Summary 574
Exercises 574
Key Concepts 575
References 576
Chapter 10
DATA CONTROLS 577
Consequences of Layering 669
Two-Layer Model 670
Three-Layer Model 673
Designing and Implementing a Business Object 674
Using the Business Object Programmatically 684
Using Business Objects with the ObjectDataSource 685
Four-Layer Model 689
Designing a Four-Layer Architecture 689
Modifying the Data Access Layer 692
Creating a Complex Domain Entity 696
Creating the Application Logic Layer 699
Using the Architecture in the Presentation Layer 702
Contents
xvi
Summary 714
Exercises 714
Key Concepts 715
References 716
Chapter 12
MANAGING ASP.NET STATE 717
Client-Stored State 718
View State 718
Control State 722
Hidden Fields 722
Querystrings 723
Cookies 723
Application State 725
The Global.asax File 726
Session State 727
How Does Session State Work? 728
Overview of Membership System 792
Configuring the SqlMembershipProvider 793
Using the Membership API 796
Role Management 803
Role Provider 803
Managing Roles 803
Using the Role Management API 806
Login Controls 814
Login Control 814
LoginName Control 820
LoginStatus Control 821
LoginView Control 822
ChangePassword Control 824
PasswordRecovery Control 826
CreateUserWizard Control 828
Summary 829
Exercises 830
Key Concepts 830
References 831
Chapter 14
PERSONALIZATION WITH PROFILES AND WEB PARTS 833
ASP.NET Profiles 834
Defining Profiles 834
Using Profile Data 836
Contents
xviii
How Do Profiles Work? 840
Saving and Retrieving Profile Data 842
Using Custom Types 843
Working with Anonymous Users 846
Guidelines for Creating Web Services 947
Summary 948
Contents
xix
Exercises 949
Key Concepts 950
References 950
Chapter 16
INTERNATIONALIZATION AND DEPLOYMENT 951
Internationalizing a Web Application 952
Introducing Resource Files 952
Generating Resource Files 954
Localizing Resource Files 959
Global Resources 964
Page-Level Culture Settings 966
Deployment 972
Manually Copy Files from Development to
Deployment Machine 972
Precompiling a Web Site 975
Creating an Installation Program Using Web Setup Project 981
Summary 987
Exercises 988
Key Concepts 988
References 988
Appendix
ASP.NET AJAX SNEAK PEEK 991
Introducing Atlas 992
Installing Atlas 994
Atlas Architecture 996
Atlas Server Features 1000
applications using ASP.NET 2.0 less daunting for readers who are unfamiliar with
ASP.NET, as well as for readers who are somewhat familiar with ASP.NET but want
to learn how to use it more effectively.
Preface
xxii
As part of the process of learning how to create realistic Web applications, this
book also endeavors to stress the importance of proper programming and design
principles. When first learning ASP.NET, a developer is often tempted to focus all
of her attention on using the many different Web server controls along with the
Visual Studio Designer. This is quite understandable given the range and power of
these controls and the simplicity and functionality of Visual Studio. However, as
you create more complex “real-world” Web applications, other considerations, such
as maintainability, scalability, and adaptability, become progressively more impor-
tant. As a consequence, this book’s ultimate aim is to help the reader (you) become
not only proficient with ASP.NET 2.0, but also to help you become a better Web
application developer by also focusing on contemporary best practices in Web
application development.
Target Audience
This book is intended first and foremost for professional developers who desire to
learn how to create Web applications using the latest version of Microsoft’s
ASP.NET. Because I teach Web development at a college, this book is also intended
for potential use in the classroom for upper-level students taking a course in Web
application development using ASP.NET.
Prerequisites
This book assumes that the reader already knows the basics of HTML and CSS. It
does not assume any knowledge of ASP.NET or C#. The book does assume that you
are familiar with programming using an object-oriented language. As a result, this
book does not provide detailed coverage of C# (for that, see Stephen Perry’s Core C#
and .NET from Prentice Hall, 2006); instead, the book illustrates how to use C# in
conjunction with ASP.NET.
knowledge of the material presented in earlier chapters. However, the material is
presented in such a way that a reader can take a more “random” approach, skipping
forward and backward to the material that is of interest to her.
The approach and sequence of topics in this book were chosen principally as a
result of my experience teaching ASP.NET in the classroom to undergraduates, as
well as to professional developers. It was also influenced by my own experiences
using ASP.NET professionally for real-world clients.
Overview
This book is broken into three principal parts. The first part is “Core ASP.NET,”
and consists of the first seven chapters of the book. These chapters introduce and
explore the key fundamental features of ASP.NET. The second part encompasses
the next five chapters: “Working with Data.” It focuses on perhaps the most impor-
tant aspect of any Web application: representing, extracting, manipulating, and
presenting data to the user. The third and final part contains four chapters: “Imple-
menting Web Applications.” Its focus is the more advanced side of application
development with ASP.NET: security, personalization, Web services, and localiza-
tion and deployment.
Chapter 1 introduces ASP.NET and the .NET Framework. It examines the
different components of the .NET platform, compares ASP.NET to other Web
Preface
xxiv
development environments, describes the ASP.NET compilation model, examines
the event system in ASP.NET, and illustrates how to create simple ASP.NET pages
using Visual Studio 2005.
Chapter 2 continues the coverage of the basics of ASP.NET. This chapter exam-
ines in depth how ASP.NET works. It describes the event system in ASP.NET, the
page lifecycle, and the essential mechanisms of postback and view state. It also cov-
ers some more advanced topics that could be skipped and returned to after you
become more comfortable with ASP.NET. These topics include the ASP.NET com-
pilation model, the
Repeater, DataList, FormView, DetailsView, and Grid-
View
controls. Each of these controls uses data binding to display (and for some even
edit) multiple sets of data in different ways.
Chapter 11 shifts the focus away from individual controls and classes and
instead examines some of the issues involved in creating a more complex Web
application with ASP.NET. It begins with the design of Web applications and some