简介:Summary of Contents
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
1. Introducing ASP.NET and the .NET Platform . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2. ASP.NET Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3. VB and C# Programming Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
4. Constructing ASP.NET Web Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
5. Building Web Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
6. Using the Validation Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
7. Database Design and Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
8. Speaking SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
9. ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
10. Displaying Content Using Data Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
11. Managing Content Using GridView and DetailsView . . . . . . . . . . . . 463
12. Advanced Data Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
13. Security and User Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569
14. Working with Files and Email . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615
15. Introduction to LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655
16. Introduction to MVC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671
17. ASP.NET AJAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701
A. Web Control Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723
B. Deploying ASP.NET Websites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775
BUILD YOUR OWN
ASP.NET 4 WEBSITE
USING C# & VB
BY CRISTIAN DARIE
WYATT BARNETT
TIM POSEY
4TH EDITION
Build Your Own ASP.NET 4 Website Using C# & VB
by Cristian Darie, Wyatt Barnett, and Tim Posey
Copyright © 2011 SitePoint Pty. Ltd.
Expert Reviewer: Pranav Rastogi Editor: Sarah Broomhall
Product Editor: Simon Mackie Index Editor: Michelle Combs
Technical Editor: Ricky Onsman Cover Design: Alex Walker
Latest Update: Fourth Edition: September
2011
Printing History:
First Edition: April 2004
Second Edition: October 2006
Third Edition: September 2008
Notice of Rights
All rights reserved. No part of this book may be reproduced, stored in a retrieval system or transmitted
in any form or by any means, without the prior written permission of the publisher, except in the case
of brief quotations embodied in critical articles or reviews.
Notice of Liability
The author and publisher have made every effort to ensure the accuracy of the information herein.
However, the information contained in this book is sold without warranty, either express or implied.
Neither the authors and SitePoint Pty. Ltd., nor its dealers or distributors, will be held liable for any
damages to be caused either directly or indirectly by the instructions contained in this book, or by the
software or hardware products described herein.
Trademark Notice
Rather than indicating every occurrence of a trademarked name as such, this book uses the names only
in an editorial fashion and to the benefit of the trademark owner with no intention of infringement of
the trademark.
Published by SitePoint Pty. Ltd.
48 Cambridge Street Collingwood
VIC Australia 3066.
Web: www.sitepoint.com
Email: business@sitepoint.com
ISBN 978-0-9870908-6-7 (print)
ISBN 978-0-9871530-3-6 (ebook)
Printed and bound in the United States of America
iv
About the Authors
Cristian Darie is a software engineer with experience in a wide range of modern technologies,
and the author of numerous technical books, including the popular Beginning E-Commerce
series. He initially tasted programming success with a prize in his first programming contest
at the age of 12. From there, Cristian moved on to many other similar achievements, and is
now studying distributed application architectures for his PhD.
Wyatt Barnett leads the in-house development team for a major industry trade association
in Washington DC. When not slinging obscene amounts of C# and SQL at a few exceedingly
large monitors, he is most often spotted staring at HDTV and other forms of entertainment
in local watering holes.
Tim Posey is a long-time developer and a passionate educator. Armed with a B.S. in Computer
Science and an M.B.A. in Finance, he has traversed many industries, consulting for multiple
corporations in banking, insurance, energy, and various e-commerce industries. As a serial
entrepreneur, he mentors local startups and non-profit organizations. He serves as a senior
software engineer at a Fortune 1000 company and an Adjunct Professor of Finance for the
American Public University System. His favorite pastime is watching Alabama football. He
may be contacted at tim@timposey.net.
About the Technical Editor
Ricky Onsman is an Australian freelance web designer and jack of all trades. With a background
in information and content services, he built his first website in 1994 for a disability
information service and has been messing about on the web ever since. He is the president
of the Web Industry Professionals Association.
About SitePoint
SitePoint specializes in publishing fun, practical, and easy-to-understand content for web
professionals. Visit http://www.sitepoint.com/ to access our books, newsletters, articles, and
community forums.
v
To my family and friends.
—Cristian Darie
To my Father, whose guidance got
me this far.
—Wyatt Barnett
For LJ and Erin.
—Tim Posey
Table of Contents
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Who Should Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
What’s in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv
Where to Find Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxviii
The SitePoint Forums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxviii
The Book’s Website . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxviii
The SitePoint Newsletters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix
The SitePoint Podcast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix
Your Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxx
Conventions Used in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxx
Code Samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxx
Tips, Notes, and Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi
Chapter 1 Introducing ASP.NET and the .NET
Platform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
What is ASP.NET? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Installing the Required Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Installing Visual Web Developer 2010 Express Edition . . . . . . . . . . . . 6
Installing SQL Server Management Studio Express . . . . . . . . . . . . . . 8
Writing Your First ASP.NET Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Chapter 2 ASP.NET Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
ASP.NET Page Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Code Declaration Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Code Render Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
ASP.NET Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Server-side Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Literal Text and HTML Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
View State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Working with Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
ASP.NET Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Visual Basic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Chapter 3 VB and C# Programming Basics . . . . 47
Programming Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Control Events and Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Page Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Variables and Variable Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Conditional Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Object Oriented Programming Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Objects and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
x
Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Understanding Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Objects in .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Using Code-behind Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Chapter 4 Constructing ASP.NET Web
Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Web Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
HTML Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Using the HTML Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Web Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Standard Web Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
List Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Advanced Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Web User Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Creating a Web User Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Master Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Using Cascading Style Sheets (CSS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Types of Styles and Style Sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Chapter 5 Building Web Applications . . . . . . . . . . 159
Introducing the Dorknozzle Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Using Visual Web Developer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Meeting the Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
xi
Executing Your Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Core Web Application Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Web.config . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Global.asax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Using Application State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Working with User Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Using the Cache Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Using Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Starting the Dorknozzle Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Preparing the Sitemap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Using Themes, Skins, and Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Building the Master Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Using the Master Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Extending Dorknozzle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Debugging and Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Debugging with Visual Web Developer . . . . . . . . . . . . . . . . . . . . . . 218
Other Kinds of Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Custom Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Handling Exceptions Locally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Chapter 6 Using the Validation Controls . . . . . . 235
Client-side Validation and Server-side Validation . . . . . . . . . . . . . . . . . 236
Introducing the ASP.NET Validation Controls . . . . . . . . . . . . . . . . . . . . . 236
Enforcing Validation on the Server . . . . . . . . . . . . . . . . . . . . . . . . . 240
Using Validation Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
RequiredFieldValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
CompareValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
RangeValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
ValidationSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
xii
RegularExpressionValidator . . . . . . . . . . . . . . . . . . . . . . . . 254
CustomValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Validation Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Updating Dorknozzle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Chapter 7 Database Design and
Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
What Is a Database? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Creating Your First Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Creating a New Database Using Visual Web Developer . . . . . . . . . 277
Creating a New Database Using SQL Server Management
Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Creating Database Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Column Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Primary Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Creating the Employees Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Creating the Remaining Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Populating the Data Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Relational Database Design Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Using Database Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
Implementing Relationships in the Dorknozzle Database . . . . . . . 308
Diagrams and Table Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Chapter 8 Speaking SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Reading Data from a Single Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
xiii
Using the SELECT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Selecting Certain Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Selecting Unique Data with DISTINCT . . . . . . . . . . . . . . . . . . . . . 326
Row Filtering with WHERE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
Selecting Ranges of Values with BETWEEN . . . . . . . . . . . . . . . . . . . 330
Matching Patterns with LIKE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Using the IN Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Sorting Results Using ORDER BY . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Limiting the Number of Results with TOP . . . . . . . . . . . . . . . . . . . 334
Reading Data from Multiple Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Table Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Expressions and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Transact-SQL (T-SQL) Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
Arithmetic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
Date and Time Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Working with Groups of Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
The COUNT Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
Grouping Records Using GROUP BY . . . . . . . . . . . . . . . . . . . . . . . . 349
Filtering Groups Using HAVING . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
The SUM, AVG, MIN, and MAX Functions . . . . . . . . . . . . . . . . . . . . . . 351
Updating Existing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
The INSERT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
The UPDATE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
The DELETE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
xiv
Chapter 9 ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Introducing ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
Importing the SqlClient Namespace . . . . . . . . . . . . . . . . . . . . . 366
Defining the Database Connection . . . . . . . . . . . . . . . . . . . . . . . . . 367
Preparing the Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Executing the Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Setting Up Database Authentication . . . . . . . . . . . . . . . . . . . . . . . . 371
Reading the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
Using Parameters with Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
Bulletproofing Data Access Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Using the Repeater Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
Creating the Dorknozzle Employee Directory . . . . . . . . . . . . . . . . . . . . . 393
More Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
Inserting Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Updating Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Deleting Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
Using Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
Chapter 10 Displaying Content Using Data
Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
DataList Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Handling DataList Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Editing DataList Items and Using Templates . . . . . . . . . . . . . . . . . . . . 448
DataList and Visual Web Developer . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
Styling the DataList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
xv
Chapter 11 Managing Content Using GridView
and DetailsView . . . . . . . . . . . . . . . . . . . . . . . . 463
Using the GridView Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
Customizing the GridView Columns . . . . . . . . . . . . . . . . . . . . . . . 471
Styling the GridView with Templates, Skins, and CSS . . . . . . . . . 472
Selecting Grid Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
Using the DetailsView Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
Styling the DetailsView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
GridView and DetailsView Events . . . . . . . . . . . . . . . . . . . . . . . . . . 488
Entering Edit Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
Using Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
Updating DetailsView Records . . . . . . . . . . . . . . . . . . . . . . . . . . 500
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
Chapter 12 Advanced Data Access . . . . . . . . . . . . . . . . 507
Using Data Source Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
Binding the GridView to a SqlDataSource . . . . . . . . . . . . . . . 510
Binding the DetailsView to a SqlDataSource . . . . . . . . . . . . 519
Displaying Lists in DetailsView . . . . . . . . . . . . . . . . . . . . . . . . . . 531
More on SqlDataSource . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534
Working with Data Sets and Data Tables . . . . . . . . . . . . . . . . . . . . . . . . . 535
What Is a Data Set Made From? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
Binding DataSets to Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540
Implementing Paging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
Storing Data Sets in View State . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
Implementing Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551
Filtering Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562
Updating a Database from a Modified DataSet . . . . . . . . . . . . . . . . . . 563
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
xvi
Chapter 13 Security and User
Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . 569
Basic Security Guidelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570
Securing ASP.NET Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572
Working with Forms Authentication . . . . . . . . . . . . . . . . . . . . . . . . 574
ASP.NET Memberships and Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588
Creating the Membership Data Structures . . . . . . . . . . . . . . . . . . . 588
Using Your Database to Store Membership Data . . . . . . . . . . . . . . 590
Using the ASP.NET Web Site Configuration Tool . . . . . . . . . . . . . . 596
Creating Users and Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
Changing Password Strength Requirements . . . . . . . . . . . . . . . . . . 600
Securing Your Web Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
Using the ASP.NET Login Controls . . . . . . . . . . . . . . . . . . . . . . . . . . 605
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613
Chapter 14 Working with Files and Email . . . . . 615
Writing and Reading Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616
Setting Up Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617
Writing Content to a Text File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620
Reading Content from a Text File . . . . . . . . . . . . . . . . . . . . . . . . . . 624
Accessing Directories and Directory Information . . . . . . . . . . . . . . . . . . 628
Working with Directory and File Paths . . . . . . . . . . . . . . . . . . . . . . 632
Uploading Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635
Sending Email with ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639
Sending a Test Email . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
Creating the Company Newsletters Page . . . . . . . . . . . . . . . . . . . . 643
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653
xvii
Chapter 15 Introduction to LINQ . . . . . . . . . . . . . . . . . . 655
Extension Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657
LINQ to SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657
Updating Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661
Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 662
Directly Executing Queries from the DataContext . . . . . . . . . . . 663
Stored Procedures with LINQ-to-SQL . . . . . . . . . . . . . . . . . . . . . . . 664
Using ASP.NET and LINQ-to-SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . 667
Chapter 16 Introduction to MVC . . . . . . . . . . . . . . . . . . . 671
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 698
Chapter 17 ASP.NET AJAX . . . . . . . . . . . . . . . . . . . . . . . . . . 701
What is Ajax? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702
ASP.NET AJAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703
Using the UpdatePanel Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 704
Managing the ScriptManager Control . . . . . . . . . . . . . . . . . . . . 708
Using Triggers to Update an UpdatePanel . . . . . . . . . . . . . . . . . . . . . . 709
The ASP.NET AJAX Control Toolkit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713
The ValidatorCalloutExtender Control Extender . . . . . . . . 715
Getting Started with Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . 718
jQuery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721
Appendix A Web Control Reference . . . . . . . . . . . . . . . 723
The WebControl Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724
Standard Web Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725
xviii
AdRotator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725
BulletedList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725
Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726
Calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727
CheckBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729
CheckBoxList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729
DropDownList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 730
FileUpload . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731
HiddenField . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 732
HyperLink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 732
Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 732
ImageButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733
ImageMap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733
Label . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734
LinkButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734
ListBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735
Literal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736
MultiView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736
Panel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736
PlaceHolder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737
RadioButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737
RadioButtonList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738
TextBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739
Wizard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740
Xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744
Validation Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744
CompareValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745
CustomValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746
RangeValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 747
RegularExpressionValidator . . . . . . . . . . . . . . . . . . . . . . . . 748
xix
RequiredFieldValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 748
ValidationSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749
Navigation Web Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 750
SiteMapPath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 750
Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751
TreeView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 756
Ajax Web Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 760
ScriptManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 760
Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 761
UpdatePanel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 761
UpdateProgress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 762
Appendix B Deploying ASP.NET Websites . . . . . . . 763
ASP.NET Hosting Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763
Using Visual Web Developer Express to Deploy ASP.NET Websites . . . . 764
Deploying MVC Sites and Web Applications . . . . . . . . . . . . . . . . . . . . . . 767
ASP.NET Deployment “Gotchas” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769
Using the SQL Server Hosting Toolkit . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770
Dealing with SQL Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775
xx