Tải bản đầy đủ (.pdf) (1,539 trang)

Tài liệu về ASP.NET chuẩn bằng tiếng anh.

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (41.06 MB, 1,539 trang )

Web Development / ASP.NET
Take your web development to
the next level using ASP.NET 4
Bill
Evjen
is one of the
most active
proponents
of .NET
technologies.
He is the
founder of the
International .NET Association
(INETA), author or coauthor of
more than two dozen books,
and Global Head of Platform
Architecture at Thomson
Reuters, Lipper.
Scott Hanselman
is a principal
program manager
lead working in
the Server and
Tools Online
Division at
Microsoft.
He has a
popular blog
and weekly podcast
at www.computerzen.com and
speaks worldwide on ASP.NET.


Devin
Rader
Devin Rader
works at
Infragistics
where he
focuses on
delivering
great experiences
to developers using their controls.
He’s also a former INETA board
member.
$59.99 USA
$71.99 CAN
Evjen, Hanselman, Rader
Bill Evjen, Scott Hanselman, Devin Rader
Professional
ASP.NET 4
in C# and VB
ASP.NET 4
Professional
Wrox Programmer to Programmer™
Join the discussion @ p2p.wrox.com
in C# and VB
B
i
l
l
E
v

is
H
i
s a p
r
p
ro
g
lea
d
th
p
d
D
e
R
a
D
d
ASP.NET is about making you as productive as possible when building
fast and secure web applications. Each release of ASP.NET gets
better and removes a lot of the tedious code that you previously
needed to put in place, making common ASP.NET tasks easier. With
this book, an unparalleled team of authors walks you through the full
breadth of ASP.NET and the new and exciting capabilities of ASP.
NET 4. The authors also show you how to maximize the abundance
of features that ASP.NET offers to make your development process
smoother and more efficient.
Professional ASP.NET 4:
• Demonstrates ASP.NET built-in systems such as the membership and

role management systems
• Covers everything you need to know about working with and manipu-
lating data
• Discusses the plethora of server controls that are at your disposal
• Explores new ways to build ASP.NET, such as working with ASP.NET MVC
and ASP.NET AJAX
• Examines the full life cycle of ASP.NET, including debugging and error
handling, HTTP modules, the provider model, and more
• Features both printed and downloadable C# and VB code examples
wrox.com
Programmer Forums Join our Programmer to Programmer forums
to ask and answer programming questions about this book, join
discussions on the hottest topics in the industry, and connect
with fellow programmers from around the world.
Code Downloads Take advantage of free code samples from this
book, as well as code samples from hundreds of other books, all
ready to use.
Read More Find articles, ebooks, sample chapters, and tables of contents for hundreds
of books, and more reference resources on programming topics that matter to you.
Wrox Professional guides are planned and written by working programmers
to meet the real-world needs of programmers, developers, and IT professionals.
Focused and relevant, they address the issues technology professionals face every
day. They provide examples, practical solutions, and expert education in new
technologies, all designed to help programmers do a better job.
w
P
t
d
Related Wrox Books
Beginning ASP.NET 4: in C# and VB

ISBN: 978-0-470-50221-1
This introductory book offers helpful examples in a step-by-step format and has code examples written in both C# and
Visual Basic. With this book you will gradually build a web site example that takes you through the processes of building
basic ASP.NET web pages, adding features with pre-built server controls, designing consistent pages, displaying data, and more.
Beginning Microsoft Visual C# 2010
ISBN: 978-0-470-50226-6
Using this book, you will first cover the fundamentals such as variables, flow control, and object-oriented programming and
gradually build your skills for web and Windows programming, Windows forms, and data access. Step-by-step directions walk
you through processes and invite you to “Try it Out” at every stage. By the end, you’ll be able to write useful programming code
following the steps you’ve learned in this thorough, practical book. If you’ve always wanted to master Visual C# programming,
this book is the perfect one-stop resource.
Professional Visual Basic 2010 and .NET 4
ISBN: 978-0-470-50224-2
If you’ve already covered the basics and want to dive deep into VB and .NET topics that professional programmers use most,
this is your guide. You’ll explore all the new features of Visual Basic 2010 as well as all the essential functions that you need,
including .NET features such as LINQ to SQL, LINQ to XML, WCF, and more. Plus, you’ll examine exception handling and debugging,
Visual Studio features, and ASP.NET web programming.
Professional C# 4 and .NET 4
ISBN: 978-0-470-50225-9
After a quick refresher on C# basics, the author dream team moves on to provide you with details of language and framework
features including LINQ, LINQ to SQL, LINQ to XML, WCF, WPF, Workflow, and Generics. Coverage also spans ASP.NET programming
with C#, working in Visual Studio 2010 with C#, and more. With this book, you’ll quickly get up to date on all the newest capabilities
of C# 4.
Visual Studio 2010 and .NET 4 Six-in-One
ISBN: 978-0-470-49948-1
This comprehensive resource offers all you need to know to become productive with .NET 4. Experienced author and .NET guru
Mitchel Sellers reviews all the important features of .NET 4, including .NET charting and ASP.NET charting, ASP.NET dynamic
data and jQuery, and F#. The coverage is divided into six distinctive parts for easy navigation and offers a practical approach
and complete examples.
Professional Visual Studio 2010

ISBN: 978-0-470-54865-3
Written by an author team of veteran programmers and developers, this book gets you quickly up to speed on what you can
expect from Visual Studio 2010. Packed with helpful examples, this comprehensive guide examines the features of Visual Studio
2010 and walks you through every facet of the Integrated Development Environment (IDE), from common tasks and functions
to its powerful tools.
WPF Programmer’s Reference: Windows Presentation Foundation with C# 2010 and .NET 4
ISBN: 978-0-470-47722-9
Written by a leading expert on Microsoft graphics programming, this richly illustrated book serves as an introduction
to WPF development and explains fundamental WPF concepts. It is packed with helpful examples and progresses through
a range of topics that gradually increase in their complexity.
Visual Basic 2010 Programmer’s Reference
ISBN: 978-0-470-49983-2
Visual Basic 2010 Programmer’s Reference is a language tutorial and a reference guide to the 2010 release of Visual Basic.
The tutorial provides basic material suitable for beginners but also includes in-depth content for more advanced developers.
Get more out of
wrox.com
Programmer to Programmer

Interact
Take an active role online by participating in our
P2P forums @ p2p.wrox.com
Wrox Online Library
Hundreds of our books are available online
through Books24x7.com
Wrox Blox
Download short informational pieces and code
to keep you up to date and out of trouble!
Join the Community
Sign up for our free monthly newsletter at
newsletter.wrox.com

Browse
Ready for more Wrox? We have books and
e-books available on .NET, SQL Server, Java,
XML, Visual Basic, C#/ C++, and much more!
Contact Us.
We always like to get feedback from our readers. Have a book idea?
Need community support? Let us know by e-mailing
PROFESSIONAL ASP.NET 4
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxix
CHAPTER 1
Application and Page Frameworks � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �1
CHAPTER 2 ASP�NET Server Controls and Client-Side Scripts � � � � � � � � � � � � � � � � � � � � � � � � � � � � 49
CHAPTER 3 ASP�NET Web Server Controls � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 85
CHAPTER 4 Validation Server Controls � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 157
CHAPTER 5 Working with Master Pages � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 187
CHAPTER 6 Themes and Skins � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 217
CHAPTER 7 Data Binding � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 237
CHAPTER 8 Data Management with ADO�NET � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �309
CHAPTER 9 Querying with LINQ � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 371
CHAPTER 10 Working with XML and LINQ to XML � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �405
CHAPTER 11 Introduction to the Provider Model � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 457
CHAPTER 12 Extending the Provider Model � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 491
CHAPTER 13 Site Navigation � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 519
CHAPTER 14 Personalization � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �569
CHAPTER 15 Membership and Role Management � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 597
CHAPTER 16 Portal Frameworks and Web Parts � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �643
CHAPTER 17 HTML and CSS Design with ASP�NET � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �683
CHAPTER 18 ASP�NET AJAX � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �709
CHAPTER 19 ASP�NET AJAX Control Toolkit � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 749
CHAPTER 20 Security � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �805

CHAPTER 21 State Management � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �835
CHAPTER 22 Caching � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �865
CHAPTER 23 Debugging and Error Handling � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �895
CHAPTER 24 File I/O and Streams � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 927
CHAPTER 25 User and Server Controls � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 979
CHAPTER 26 Modules and Handlers � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 1045
CHAPTER 27 ASP�NET MVC � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 1059
CHAPTER 28 Using Business Objects � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 1081
CHAPTER 29 ADO�NET Entity Framework � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 1105
CHAPTER 30 ASP�NET Dynamic Data � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 1129
CHAPTER 31 Working with Services � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 1153
CHAPTER 32 Building Global Applications � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �1217
CHAPTER 33 Configuration � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 1239
CHAPTER 34 Instrumentation � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 1285
CHAPTER 35 Administration and Management � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 1315
CHAPTER 36 Packaging and Deploying ASP�NET Applications � � � � � � � � � � � � � � � � � � � � � � � � � � 1337
APPENDIX A Migrating Older ASP�NET Projects � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 1369
APPENDIX B ASP�NET Ultimate Tools � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 1381
APPENDIX C Silverlight 3 and ASP�NET � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 1399
APPENDIX D Dynamic Types and Languages � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 1419
APPENDIX E ASP�NET Online Resources � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 1427
INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1429
PROFESSIONAL
ASP.NET 4

PROFESSIONAL
ASP.NET 4
IN C# AND VB
Bill Evjen
Scott Hanselman

Devin Rader
Professional ASP.NET 4: In C# and VB
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2010 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-0-470-50220-4
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108
of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization
through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers,
MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the
Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011,
fax (201) 748-6008, or online at />Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with
respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including
without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or
promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work
is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional
services. If professional assistance is required, the services of a competent professional person should be sought. Neither
the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is
referred to in this work as a citation and/or a potential source of further information does not mean that the author or the
publisher endorses the information the organization or Web site may provide or recommendations it may make. Further,
readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this
work was written and when it is read.
For general information on our other products and services please contact our Customer Care Department within the

United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available
in electronic books.
Library of Congress Control Number: 2009943645
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are
trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other
countries, and may not be used without written permission. All other trademarks are the property of their respective
owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book.
To Tuija, always.
—Bill Evjen
To Momo and the boys. Toot!
—Scott Hanselman

aBouT The auThors
Bill evjen is an active proponent of .NET technologies and community-based learning
initiatives for .NET. He has been actively involved with .NET since the first bits
were released in 2000. In the same year, Bill founded the St. Louis .NET User Group
(
www.stlnet.org), one of the world’s first such groups. Bill is also the founder and former
executive director of the International .NET Association (
www.ineta.org), which represents
more than 500,000 members worldwide.
Based in St. Louis, Missouri, Bill is an acclaimed author and speaker on ASP.NET and Services. He has
authored or coauthored more than 20 books including Professional C# 2010, Professional VB 2008,
ASP.NET Professional Secrets, XML Web Services for ASP.NET, and Web Services Enhancements:
Understanding the WSE for Enterprise Applications (all published by Wiley). In addition to writing, Bill is a
speaker at numerous conferences, including DevConnections, VSLive!, and TechEd. Along with these items,
Bill works closely with Microsoft as a Microsoft Regional Director and an MVP.
Bill is the Global Head of Platform Architecture for Thomson Reuters, Lipper, the international news
and financial services company (www.thomsonreuters.com). He graduated from Western Washington

University in Bellingham, Washington, with a Russian language degree. When he isn’t tinkering on the
computer, he can usually be found at his summer house in Toivakka, Finland. You can reach Bill on Twitter
at @billevjen.
scoTT hanselman works for Microsoft as a Principal Program Manager Lead in the
Server and Tools Online Group, aiming to spread the good word about developing software,
most often on the Microsoft stack. Before this, Scott was the Chief Architect at Corillian,
an eFinance enabler, for 6+ years, and before Corillian, he was a Principal Consultant at
Microsoft Gold Partner for 7 years. He was also involved in a few things like the MVP and
RD programs and will speak about computers (and other passions) whenever someone will
listen to him. He blogs at
www.hanselman.com, podcasts at www.hanselminutes.com, and
runs a team that contributes to
www.asp.net, www.windowsclient.net, and www.silverlight.net.
Follow Scott on Twitter
@shanselman.
devin rader works at Infragistics where he focuses on delivering great experiences to
developers using their controls. He’s done work on all of the .NET platforms, but most
recently has been focused on Web technologies ASP.NET and Silverlight. As a co-founder
of the St. Louis .NET User group and a former INETA board member, and a member of
the Central New Jersey .NET user group, he’s an active supporter of the .NET developer
community. He’s also co-author or technical editor of numerous books on .NET, including
Wrox’s Silverlight 3 Programmer’s Reference. Follow Devin on Twitter
@devinrader.
ABOUT THE TECHNICAL EDITORS
CARLOS FIGUEROA has been developing and designing Web solutions for the last 8 years, participating
in international projects for the pharmaceutical industry, banking, commercial air transportation, and the
government. During these years, Carlos has been deeply involved as an early adopter of Microsoft Web
development technologies, such as ASP.NET and Silverlight.
He has been awarded Microsoft Most Valuable Professional for the last 5 years and holds the MCAD
certification. Carlos is a Senior Software Developer at Oshyn, Inc. (

www.oshyn.com), a company
specialized on delivering innovative business solutions for the web, mobile devices and emerging
technology platforms. At Oshyn, Carlos is dedicated to help some of the most recognizable brands in the
world to achieve technology success. You can reach Carlos at or follow
him on twitter @carlosfigueroa.
ANDREW MOORE is a graduate of Purdue University–Calumet in Hammond, Indiana, and has been
developing software since 1998 for radar systems, air traffic management, discrete-event simulation, and
business communications applications using C, C++, C#, and Java on the Windows, UNIX, and Linux
platforms. Andrew is also a contributor to the Wrox Blox article series.
He is currently working as a Senior Software Engineer at Interactive Intelligence, Inc., in Indianapolis,
Indiana, developing server-side applications for a multimedia unified business communications platform.
Andrew lives in Indiana with his wife Barbara and children Sophia and Andrew.
CREDITS
ACQUISITIONS EDITOR
Paul Reese
SENIOR PROJECT EDITOR
Kevin Kent
TECHNICAL EDITORS
Carlos Figueroa
Andrew Moore
PRODUCTION EDITOR
Daniel Scribner
COPY EDITOR
Paula Lowell
EDITORIAL DIRECTOR
Robyn B� Siesky
EDITORIAL MANAGER
Mary Beth Wakefield
ASSOCIATE DIRECTOR OF MARKETING
David Mayhew

PRODUCTION MANAGER
Tim Tate
VICE PRESIDENT AND EXECUTIVE GROUP
PUBLISHER
Richard Swadley
VICE PRESIDENT AND EXECUTIVE PUBLISHER
Barry Pruett
ASSOCIATE PUBLISHER
Jim Minatel
PROJECT COORDINATOR, COVER
Lynsey Stanford
PROOFREADERS
Word One
INDEXER
J & J Indexing
COVER DESIGNER
Michael E� Trent
COVER IMAGE
© Jon Feingersh Photography Inc / Blend
Images / Jupiter Images

ACKNOWLEDGMENTS
THANKS TO KEVIN KENT, PAUL REESE, AND JIM MINATEL for the opportunity to work on such a great
book. In addition to my co-authors, I would like to thank my family for putting up with all the writing.
Thank you Tuija, Sofia, Henri, and Kalle!
—Bill Evjen

CONTENTS
INTRODUCTION xxxix
CHAPTER 1: APPLICATION AND PAGE FRAMEWORKS 1

Application Location Options 1
Built-in Web Server 2
IIS 3
FTP 3
Web Site Requiring FrontPage Extensions 4
The ASP.NET Page Structure Options 4
Inline Coding 6
Code-Behind Model 7
ASP.NET 4 Page Directives 9
@Page 10
@Master 13
@Control 14
@Import 15
@Implements 17
@Register 17
@Assembly 18
@PreviousPageType 18
@MasterType 18
@OutputCache 18
@Reference 19
ASP.NET Page Events 19
Dealing with Postbacks 20
Cross-Page Posting 21
ASP.NET Application Folders 26
App_Code Folder 27
App_Data Folder 30
App_Themes Folder 31
App_GlobalResources Folder 31
App_LocalResources Folder 31
App_WebReferences Folder 31

App_Browsers Folder 31
Compilation 32
Build Providers 35
xvi
CONTENTS
Using the Built-in Build Providers 36
Using Your Own Build Providers 36
Global.asax 41
Working with Classes Through Visual Studio 2010 44
Summary 47
CHAPTER 2: ASP.NET SERVER CONTROLS AND CLIENTSIDE SCRIPTS 49
ASP.NET Server Controls 49
Types of Server Controls 50
Building with Server Controls 51
Working with Server Control Events 53
Applying Styles to Server Controls 54
Examining the Controls’ Common Properties 54
Changing Styles Using Cascading Style Sheets 56
CSS Changes in ASP�NET 4 58
HTML Server Controls 59
Looking at the HtmlControl Base Class 60
Looking at the HtmlContainerControl Class 61
Looking at All the HTML Classes 61
Using the HtmlGenericControl Class 62
Identifying ASP.NET Server Controls 63
Manipulating Pages and Server Controls with JavaScript 65
Using Page�ClientScript�RegisterClientScriptBlock 66
Using Page�ClientScript�RegisterStartupScript 68
Using Page�ClientScript�RegisterClientScriptInclude 69
Client-Side Callback 69

Comparing a Typical Postback to a Callback 70
Using the Callback Feature — A Simple Approach 72
Using the Callback Feature with a Single Parameter 75
Using the Callback Feature — A More Complex Example 78
Summary 83
CHAPTER 3: ASP.NET WEB SERVER CONTROLS 85
An Overview of Web Server Controls 85
The Label Server Control 86
The Literal Server Control 88
The TextBox Server Control 88
Using the Focus() Method 89
Using AutoPostBack 89
Using AutoCompleteType 90
xvii
CONTENTS
The Button Server Control 91
The CausesValidation Property 91
The CommandName Property 92
Buttons That Work with Client-Side JavaScript 93
The LinkButton Server Control 94
The ImageButton Server Control 95
The HyperLink Server Control 96
The DropDownList Server Control 96
Visually Removing Items from a Collection 98
The ListBox Server Control 100
Allowing Users to Select Multiple Items 100
An Example of Using the ListBox Control 100
Adding Items to a Collection 102
The CheckBox Server Control 102
How to Determine Whether Check Boxes Are Checked 103

Assigning a Value to a Check Box 104
Aligning Text Around the Check Box 104
The CheckBoxList Server Control 104
The RadioButton Server Control 106
The RadioButtonList Server Control 108
Image Server Control 109
Table Server Control 110
The Calendar Server Control 112
Making a Date Selection from the Calendar Control 112
Choosing a Date Format to Output from the Calendar 113
Making Day, Week, or Month Selections 114
Working with Date Ranges 115
Modifying the Style and Behavior of Your Calendar 116
AdRotator Server Control 119
The Xml Server Control 121
Panel Server Control 122
The PlaceHolder Server Control 124
BulletedList Server Control 124
HiddenField Server Control 129
FileUpload Server Control 130
Uploading Files Using the FileUpload Control 130
Giving ASP�NET Proper Permissions to Upload Files 132
Understanding File Size Limitations 133
Uploading Multiple Files from the Same Page 134
Placing the Uploaded File into a Stream Object 137
Moving File Contents from a Stream Object to a Byte Array 137
xviii
CONTENTS
MultiView and View Server Controls 138
Wizard Server Control 141

Customizing the Side Navigation 142
Examining the AllowReturn Attribute 143
Working with the StepType Attribute 143
Adding a Header to the Wizard Control 143
Working with the Wizard’s Navigation System 144
Utilizing Wizard Control Events 145
Using the Wizard Control to Show Form Elements 146
ImageMap Server Control 150
Chart Server Control 152
Summary 155
CHAPTER 4: VALIDATION SERVER CONTROLS 157
Understanding Validation 157
Client-Side versus Server-Side Validation 158
ASP.NET Validation Server Controls 159
Validation Causes 160
The RequiredFieldValidator Server Control 160
The CompareValidator Server Control 164
The RangeValidator Server Control 167
The RegularExpressionValidator Server Control 171
The CustomValidator Server Control 172
The ValidationSummary Server Control 176
Turning O Client-Side Validation 179
Using Images and Sounds for Error Notifications 180
Working with Validation Groups 181
Summary 185
CHAPTER 5: WORKING WITH MASTER PAGES 187
Why Do You Need Master Pages? 187
The Basics of Master Pages 189
Coding a Master Page 190
Coding a Content Page 192

Mixing Page Types and Languages 196
Specifying Which Master Page to Use 197
Working with the Page Title 198
Working with Controls and Properties from the Master Page 199
Specifying Default Content in the Master Page 205
Programmatically Assigning the Master Page 207
xix
CONTENTS
Nesting Master Pages 207
Container-Specific Master Pages 211
Event Ordering 212
Caching with Master Pages 213
ASP.NET AJAX and Master Pages 214
Summary 216
CHAPTER 6: THEMES AND SKINS 217
Using ASP.NET Themes 217
Applying a Theme to a Single ASP�NET Page 217
Applying a Theme to an Entire Application 219
Removing Themes from Server Controls 219
Removing Themes from Web Pages 220
Understanding Themes When Using Master Pages 221
Understanding the StyleSheetTheme Attribute 221
Creating Your Own Themes 222
Creating the Proper Folder Structure 222
Creating a Skin 222
Including CSS Files in Your Themes 224
Having Your Themes Include Images 227
Defining Multiple Skin Options 229
Programmatically Working with Themes 231
Assigning the Page’s Theme Programmatically 231

Assigning a Control’s SkinID Programmatically 231
Themes, Skins, and Custom Controls 232
Summary 235
CHAPTER 7: DATA BINDING 237
Data Source Controls 237
SqlDataSource Control 239
AccessDataSource Control 247
LinqDataSource Control 247
EntityDataSource Control 253
XmlDataSource Control 255
ObjectDataSource Control 256
SiteMapDataSource Control 259
Data Source Control Caching 259
Storing Connection Information 260
Using Bound List Controls with Data Source Controls 262
GridView 262
xx
CONTENTS
Editing GridView Row Data 275
Deleting GridView Data 281
Other GridView Formatting Features 283
DetailsView 283
Inserting, Updating, and Deleting Data Using DetailsView 287
ListView 289
FormView 296
Other Databound Controls 300
TreeView 300
AdRotator 301
Menu 301
Inline Data-Binding Syntax 302

Data-Binding Syntax 302
XML Data Binding 303
Expressions and Expression Builders 303
Summary 308
CHAPTER 8: DATA MANAGEMENT WITH ADO.NET 309
Basic ADO.NET Features 310
Common ADO�NET Tasks 310
Basic ADO�NET Namespaces and Classes 314
Using the Connection Object 315
Using the Command Object 317
Using the DataReader Object 318
Using DataAdapter 320
Using Parameters 322
Understanding DataSet and DataTable 325
Using Oracle as Your Database with ASP�NET 329
The DataList Server Control 330
Looking at the Available Templates 331
Working with ItemTemplate 331
Working with Other Layout Templates 334
Working with Multiple Columns 336
The ListView Server Control 336
Connecting the ListView to a Database 337
Creating the Layout Template 338
Creating the ItemTemplate 340
Creating the EditItemTemplate 341
Creating the EmptyItemTemplate 341
Creating the InsertItemTemplate 341
Viewing the Results 342
xxi
CONTENTS

Using Visual Studio for ADO.NET Tasks 344
Creating a Connection to the Data Source 344
Working with a Dataset Designer 345
Using the CustomerOrders DataSet 348
Asynchronous Command Execution 352
Asynchronous Methods of the SqlCommand Class 352
IAsyncResult Interface 353
AsyncCallback 354
WaitHandle Class 354
Approaches of Asynchronous Processing in ADO�NET 355
Canceling Asynchronous Processing 370
Asynchronous Connections 370
Summary 370
CHAPTER 9: QUERYING WITH LINQ 371
LINQ to Objects 371
Understanding Traditional Query Methods 371
Replacing Traditional Queries with LINQ 378
Data Grouping 384
Using Other LINQ Operators 385
Making LINQ Joins 385
Paging Using LINQ 386
LINQ to XML 387
Joining XML Data 390
LINQ to SQL 391
Making Insert, Update, and Delete Queries through LINQ 399
Extending LINQ 403
Summary 403
CHAPTER 10: WORKING WITH XML AND LINQ TO XML 405
The Basics of XML 406
The XML InfoSet 408

XSD–XML Schema Definition 409
Editing XML and XML Schema in Visual Studio 2010 410
XmlReader and XmlWriter 413
Using XDocument Rather Than XmlReader 415
Using Schema with XmlTextReader 416
Validating Against a Schema Using an XDocument 417
Including NameTable Optimization 419
Retrieving �NET CLR Types from XML 420
xxii
CONTENTS
ReadSubtree and XmlSerialization 422
Creating CLR Objects from XML with LINQ to XML 423
Creating XML with XmlWriter 424
Creating XML with LINQ for XML 426
Improvements for XmlReader and XmlWriter 429
XmlDocument and XPathDocument 429
Problems with the DOM 429
XPath, the XPathDocument, and XmlDocument 430
DataSets 434
Persisting DataSets to XML 434
XmlDataDocument 435
The XmlDataSource Control 437
XSLT 441
XslCompiledTransform 442
XSLT Debugging 445
Databases and XML 446
FOR XML AUTO 446
SQL Server and the XML Data Type 451
Summary 456
CHAPTER 11: INTRODUCTION TO THE PROVIDER MODEL 457

Understanding the Provider 458
The Provider Model in ASP.NET 4 459
Setting Up Your Provider to Work with Microsoft SQL Server
7�0, 2000, 2005, or 2008 461
Membership Providers 466
Role Providers 469
The Personalization Provider 473
The SiteMap Provider 474
SessionState Providers 476
Web Event Providers 478
Configuration Providers 485
The Web Parts Provider 487
Configuring Providers 489
Summary 489
CHAPTER 12: EXTENDING THE PROVIDER MODEL 491
Providers Are One Tier in a Larger Architecture 491
Modifying Through Attribute-Based Programming 492
Simpler Password Structures Through the SqlMembershipProvider 492
Stronger Password Structures Through the SqlMembershipProvider 495
xxiii
CONTENTS
Examining ProviderBase 496
Building Your Own Providers 497
Creating the CustomProviders Application 498
Constructing the Class Skeleton Required 499
Creating the XML User Data Store 502
Defining the Provider Instance in the web�config File 502
Not Implementing Methods and Properties of the
MembershipProvider Class 503
Implementing Methods and Properties of

the MembershipProvider Class 504
Using the XmlMembershipProvider for User Login 510
Extending Pre-Existing Providers 511
Limiting Role Capabilities with a New LimitedSqlRoleProvider Provider 511
Using the New LimitedSqlRoleProvider Provider 515
Summary 518
CHAPTER 13: SITE NAVIGATION 519
XML-Based Sitemaps 520
SiteMapPath Server Control 521
The PathSeparator Property 523
The PathDirection Property 525
The ParentLevelsDisplayed Property 525
The ShowToolTips Property 526
The SiteMapPath Control’s Child Elements 526
TreeView Server Control 527
Identifying the TreeView Control’s Built-In Styles 530
Examining the Parts of the TreeView Control 531
Binding the TreeView Control to an XML File 532
Selecting Multiple Options in a TreeView 534
Specifying Custom Icons in the TreeView Control 537
Specifying Lines Used to Connect Nodes 538
Working with the TreeView Control Programmatically 540
Menu Server Control 545
Applying Dierent Styles to the Menu Control 546
Using Menu Events 550
Binding the Menu Control to an XML File 551
SiteMap Data Provider 552
ShowStartingNode 552
StartFromCurrentNode 553
StartingNodeOset 554

StartingNodeUrl 555
SiteMap API 555

×