Tải bản đầy đủ (.pdf) (219 trang)

Tài liệu Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition docx

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 (7.31 MB, 219 trang )

SITEPOINT BOOKS
Advocate best practice techniques
Lead you through practical examples
Provide working code for your web site
Make learning easy and fun
ABOUT CRISTIAN DARIE
Wyatt leads the in-house development
team for a major industry trade association.
When not slinging obscene amounts of
C# and SQL at a few exceedingly large
monitors, he is sharing his ASP.NET
wisdom with others.
Cristian is a professional software
engineer with vast experience in a wide
range of modern technologies. The
author of numerous technical books,
Cristian has mastered the complexities of
ASP.NET, so you don’t have to.
ABOUT WYATT BARNETT
PANTONE 2955 CPANTONE Orange 021 C
CMYK 100, 45, 0, 37CMYK O, 53, 100, 0
Black 100%Black 50%
#-9+
0ANTONE
'REYSCALE
PANTONE 2955 CPANTONE Orange 021 C
CMYK 100, 45, 0, 37CMYK O, 53, 100, 0
Black 100%Black 50%
#-9+
0ANTONE
'REYSCALE


WEB PROGRAMMING
ISBN 978-0-9804552-1-2
USD $44.95 CAD $44.95
Visit us on the Web at sitepoint.com or for sales and support email
ALL SOURCE CODE AVAILABLE FOR DOWNLOAD
Build Your Own ASP.NET 3.5 Web Site Using C# & VB
is packed full of practical examples, straightforward
explanations, and ready-to-use code samples in both C# and
VB. The third edition of this comprehensive step-by-step guide
will help get your database-driven ASP.NET web site up and
running in no time.
Learn how to set up the free software you need for ASP.NET.
Choose either C# or VB: this book covers both languages.
Build a complete web site from scratch using ASP.NET.
Use the code examples for your future ASP.NET projects.
Learn how to easily integrate data from Microsoft SQL Server.
START BUILDING POWERFUL ASP.NET
WEB APPLICATIONS TODAY!
ASP.NET 3.5
BUILD YOUR OWN
ASP.NET 3.5
WEB SITE
USING C# & VB
DARIE &
BARNETT
The book not only satisfies , it excites
A must-read for
avid beginners”
aspalliance.com
I have yet to find a

better book”
csharp-online.net
The book not only
satisfies it
excites”
aspnetpro.com
coveraspnet3.indd 1 8/27/2008 11:04:40 AM
BUILD YOUR OWN
ASP.NET 3.5
WEB SITE
USING C# & VB
BY CRISTIAN DARIE
& WYATT BARNETT
3RD EDITION
THE ULTIMATE ASP.NET BEGINNER’S GUIDE
Thank you for downloading the sample chapters of Build Your Own ASP.NET 3.5
Web Site Using C# & VB published by SitePoint.
This excerpt includes the Summary of Contents, Information about the Author,
Editors and SitePoint, Table of Contents, Preface, four sample chapters from the book,
and the index.
We hope you find this information useful in evaluating this book.
For more information or to order, visit sitepoint.com
Summary of Contents of this Excerpt
Preface xxi
I. Introducing ASP.NET and the .NET Platform 1
2. ASP.NET Basics 25
3. VB and C# Programming Basics 45
4. Constructing ASP.NET Web Pages 93
Index 701
Summary of Additional Book Contents

5. Building Web Applications 153
6. Using the Validation Controls 223
7. Database Design and Development 259
8. Speaking SQL 303
9. ADO.NET 343
10. Displaying Content Using Data Lists 413
11. Managing Content Using Grid View and Details View 441
12. Advanced Data Access 483
13. Security and User Authentication 545
14. Working with Files and Email 591
15. ASP.NET AJAX 631
A. Web Control Reference 651
B. Deploying ASP.NET Web Sites 691

BUILD YOUR OWN
ASP.NET 3.5 WEB
SITE USING C# & VB
BY CRISTIAN DARIE
& WYATT BARNETT
3RD EDITION
iv
Build Your Own ASP.NET 3.5 Web Site Using C# & VB
by Cristian Darie and Wyatt Barnett
Copyright © 2008 SitePoint Pty. Ltd.
Expert Reviewer: Wyatt Barnett Editor: Georgina Laidlaw
Managing Editor: Chris Wyness Index Editor: Russell Brooks
Technical Editor: Andrew Tetlaw Cover Design: Alex Walker
Cover Image: Lucas Chan
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:
ISBN 978-0-9804552-1-2
Printed and bound in the United States of America
v
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. Having worked with computers since he was old enough to use a keyboard, 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.
He always loves hearing feedback about his books, so don't hesitate to drop him a “hello”
message when you have a spare moment. Cristian can be contacted through his personal web
site at .
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. He also writes for SitePoint's .NET blog,
The Daily Catch.
1
About the Technical Editor
Andrew Tetlaw has been tinkering with web sites as a web developer since 1997 and has
also worked as a high school English teacher, an English teacher in Japan, a window cleaner,
a car washer, a kitchen hand, and a furniture salesman. At SitePoint he is dedicated to making
the world a better place through the technical editing of SitePoint books, kits, and articles.
He is also a busy father of five, enjoys coffee, and often neglects his blog at
About the Technical Director
As Technical Director for SitePoint, Kevin Yank oversees all of its technical publica-
tions—books, articles, newsletters, and blogs. He has written over 50 articles for SitePoint,
but is best known for his book, Build Your Own Database Driven Website Using PHP &
MySQL. Kevin lives in Melbourne, Australia, and enjoys performing improvised comedy
theater and flying light aircraft.
About SitePoint
SitePoint specializes in publishing fun, practical, and easy-to-understand content for web
professionals. Visit to access our books, newsletters, articles, and
community forums.
1


To my family and friends.

—Cristian Darie
To my Father, whose guidance got
me this far.
—Wyatt Barnett

Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Who Should Read This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
What’s in This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii
The Book’s Web Site . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
The Code Archive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvi
Updates and Errata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvi
The SitePoint Forums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvi
The SitePoint Newsletters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii
Your Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii
Conventions Used in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii
Code Samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii
Tips, Notes, and Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxviii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix
Chapter 1 Introducing ASP.NET and the .NET
Platform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
What is ASP.NET? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Installing the Required Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Installing Visual Web Developer 2008 Express Edition . . . . . . . . . . . . 6
Installing SQL Server Management Studio Express . . . . . . . . . . . . . . 8
Writing Your First ASP.NET Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Chapter 2 ASP.NET Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
ASP.NET Page Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

x
Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Code Declaration Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Code Render Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
ASP.NET Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Server-side Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Literal Text and HTML Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
View State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Working with Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
ASP.NET Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Visual Basic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Chapter 3 VB and C# Programming Basics . . . . 45
Programming Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Control Events and Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Page Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Variables and Variable Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Conditional Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Object Oriented Programming Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Objects and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80

Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
xi
Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Understanding Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Objects in .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Using Code-behind Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Chapter 4 Constructing ASP.NET Web
Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Web Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
HTML Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Using the HTML Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Web Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Standard Web Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
List Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Advanced Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Web User Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Creating a Web User Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Master Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Using Cascading Style Sheets (CSS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Types of Styles and Style Sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Chapter 5 Building Web Applications . . . . . . . . . . 153
Introducing the Dorknozzle Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Using Visual Web Developer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Meeting the Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Executing Your Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Core Web Application Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Order the print version of this book to get all 700+ pages!

xii
Web.config . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Global.asax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Using Application State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Working with User Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Using the Cache Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Using Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Starting the Dorknozzle Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Preparing the Sitemap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Using Themes, Skins, and Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Building the Master Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Using the Master Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Extending Dorknozzle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Debugging and Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Debugging with Visual Web Developer . . . . . . . . . . . . . . . . . . . . . . 207
Other Kinds of Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Custom Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Handling Exceptions Locally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Chapter 6 Using the Validation Controls . . . . . . 223
Client-side Validation and Server-side Validation . . . . . . . . . . . . . . . . . 224
Introducing the ASP.NET Validation Controls . . . . . . . . . . . . . . . . . . . . . 224
Enforcing Validation on the Server . . . . . . . . . . . . . . . . . . . . . . . . . 228
Using Validation Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
RequiredFieldValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
CompareValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
RangeValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
ValidationSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
RegularExpressionValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
CustomValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245

Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
xiii
Validation Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Updating Dorknozzle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Chapter 7 Database Design and
Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
What Is a Database? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Creating Your First Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Creating a New Database Using Visual Web Developer . . . . . . . . . 263
Creating a New Database Using SQL Server Management
Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Creating Database Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Column Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Primary Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Creating the Employees Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Creating the Remaining Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Populating the Data Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Relational Database Design Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Using Database Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Implementing Relationships in the Dorknozzle Database . . . . . . . 293
Diagrams and Table Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Chapter 8 Speaking SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Reading Data from a Single Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
Using the SELECT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Selecting Certain Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Order the print version of this book to get all 700+ pages!

xiv
Selecting Unique Data with DISTINCT . . . . . . . . . . . . . . . . . . . . . 310
Row Filtering with WHERE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Selecting Ranges of Values with BETWEEN . . . . . . . . . . . . . . . . . . . 313
Matching Patterns with LIKE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Using the IN Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Sorting Results Using ORDER BY . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Limiting the Number of Results with TOP . . . . . . . . . . . . . . . . . . . 317
Reading Data from Multiple Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Table Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Expressions and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Transact-SQL Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Arithmetic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Date and Time Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Working with Groups of Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
The COUNT Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
Grouping Records Using GROUP BY . . . . . . . . . . . . . . . . . . . . . . . . 330
Filtering Groups Using HAVING . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
The SUM, AVG, MIN, and MAX Functions . . . . . . . . . . . . . . . . . . . . . . 333
Updating Existing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
The INSERT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
The UPDATE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
The DELETE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Chapter 9 ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
Introducing ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Importing the SqlClient Namespace . . . . . . . . . . . . . . . . . . . . . 346

Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
xv
Defining the Database Connection . . . . . . . . . . . . . . . . . . . . . . . . . 347
Preparing the Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
Executing the Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Setting Up Database Authentication . . . . . . . . . . . . . . . . . . . . . . . . 351
Reading the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
Using Parameters with Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
Bulletproofing Data Access Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
Using the Repeater Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Creating the Dorknozzle Employee Directory . . . . . . . . . . . . . . . . . . . . . 372
More Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
Inserting Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
Updating Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Deleting Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
Using Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Chapter 10 Displaying Content Using Data
Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
DataList Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Handling DataList Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
Editing DataList Items and Using Templates . . . . . . . . . . . . . . . . . . . . 426
DataList and Visual Web Developer . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Styling the DataList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
Chapter 11 Managing Content Using Grid
View and Details View . . . . . . . . . . . . . . . . 441
Using the GridView Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
Customizing the GridView Columns . . . . . . . . . . . . . . . . . . . . . . . 449
Order the print version of this book to get all 700+ pages!

xvi
Styling the GridView with Templates, Skins, and CSS . . . . . . . . . . 450
Selecting Grid Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
Using the DetailsView Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
Styling the DetailsView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
GridView and DetailsView Events . . . . . . . . . . . . . . . . . . . . . . . . . . 465
Entering Edit Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
Using Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
Updating DetailsView Records . . . . . . . . . . . . . . . . . . . . . . . . . . 476
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
Chapter 12 Advanced Data Access . . . . . . . . . . . . . . . . 483
Using Data Source Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
Binding the GridView to a SqlDataSource . . . . . . . . . . . . . . . . . . 486
Binding the DetailsView to a SqlDataSource . . . . . . . . . . . . . . 495
Displaying Lists in DetailsView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
More on SqlDataSource . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510
Working with Data Sets and Data Tables . . . . . . . . . . . . . . . . . . . . . . . . . 511
What Is a Data Set Made From? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
Binding DataSets to Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
Implementing Paging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
Storing Data Sets in View State . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
Implementing Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526
Filtering Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
Updating a Database from a Modified DataSet . . . . . . . . . . . . . . . . . . . 538
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
Chapter 13 Security and User
Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
Basic Security Guidelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
xvii

Securing ASP.NET Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
Working with Forms Authentication . . . . . . . . . . . . . . . . . . . . . . . . 550
ASP.NET Memberships and Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
Creating the Membership Data Structures . . . . . . . . . . . . . . . . . . . 564
Using Your Database to Store Membership Data . . . . . . . . . . . . . . 566
Using the ASP.NET Web Site Configuration Tool . . . . . . . . . . . . . . 572
Creating Users and Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
Changing Password Strength Requirements . . . . . . . . . . . . . . . . . . 576
Securing Your Web Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579
Using the ASP.NET Login Controls . . . . . . . . . . . . . . . . . . . . . . . . . . 581
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589
Chapter 14 Working with Files and Email . . . . . 591
Writing and Reading Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592
Setting Up Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
Writing Content to a Text File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596
Reading Content from a Text File . . . . . . . . . . . . . . . . . . . . . . . . . . 600
Accessing Directories and Directory Information . . . . . . . . . . . . . . . . . . 604
Working with Directory and File Paths . . . . . . . . . . . . . . . . . . . . . . 608
Uploading Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611
Sending Email with ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615
Sending a Test Email . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617
Creating the Company Newsletters Page . . . . . . . . . . . . . . . . . . . . 619
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629
Chapter 15 ASP.NET AJAX . . . . . . . . . . . . . . . . . . . . . . . . . . 631
What is Ajax? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632
ASP.NET AJAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633
Using the UpdatePanel Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634
Managing the ScriptManager Control . . . . . . . . . . . . . . . . . . . . 638
Order the print version of this book to get all 700+ pages!
xviii

Using Triggers to Update an UpdatePanel . . . . . . . . . . . . . . . . . . . . . . . . 639
The ASP.NET AJAX Control Toolkit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
The ValidatorCalloutExtender Control Extender . . . . . . . . 645
Getting Started with Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . 648
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650
Appendix A Web Control Reference . . . . . . . . . . . . . . . 651
The WebControl Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652
Standard Web Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653
AdRotator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653
BulletedList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653
Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 654
Calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655
CheckBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657
CheckBoxList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657
DropDownList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 659
FileUpload . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 659
HiddenField . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660
HyperLink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660
Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660
ImageButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661
ImageMap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 662
Label . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 662
LinkButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 662
ListBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663
Literal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664
MultiView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664
Panel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665
Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)

xix
PlaceHolder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665
RadioButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665
RadioButtonList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666
TextBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667
Wizard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668
Xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672
Validation Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672
CompareValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673
CustomValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 674
RangeValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675
RegularExpressionValidator . . . . . . . . . . . . . . . . . . . . . . . . 676
RequiredFieldValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676
ValidationSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677
Navigation Web Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 678
SiteMapPath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 678
Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679
TreeView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 684
Ajax Web Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 688
ScriptManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 688
Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689
UpdatePanel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689
UpdateProgress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 690
Appendix B Deploying ASP.NET Web Sites . . . . . 691
ASP.NET Hosting Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691
Using Visual Web Developer Express to Deploy ASP.NET
Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692
ASP.NET Deployment “Gotchas” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695
Using the SQL Server Hosting Toolkit . . . . . . . . . . . . . . . . . . . . . . . . . . . . 696
Dealing with SQL Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 698

Order the print version of this book to get all 700+ pages!
xx
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701
Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
Preface
Web development is very exciting. There’s nothing like the feeling you have after
you place your first dynamic web site online, and see your little toy in action while
other people are actually using it!
Web development with ASP.NET is particularly exciting. If you’ve never created a
dynamic web site before, I’m sure you’ll fall in love with this area of web develop-
ment. If you’ve worked with other server-side technologies, I expect you’ll be a little
shocked by the differences.
ASP.NET really is a unique technology, and it provides new and extremely efficient
ways to create web applications using the programming language with which you
feel most comfortable. Though it can take some time to learn, ASP.NET is simple
to use. Whether you want to create simple web forms, feature-rich shopping carts,
or even complex enterprise applications, ASP.NET can help you do it. All the tools
you’ll need to get up and running are immediately available and easy to install, and
require very little initial configuration.
This book will be your gentle introduction to the wonderful world of ASP.NET,
teaching you the foundations step by step. First, you’ll learn the theory; then, you’ll
put it into practice as we work through practical exercises together. To demonstrate
some of the more complex functionality, and to put the theory into a cohesive,
realistic context, we’ll develop a project through the course of this book. The pro-
ject—an intranet site for a company named Dorknozzle—will allow us to see the
many components of .NET in action, and to understand through practice exactly
how .NET works in the real world.
We hope you’ll find reading this book an enjoyable experience that will significantly
help you with your future web development projects!
Who Should Read This Book?

This book is aimed at beginner, intermediate, and advanced web designers looking
to make the leap into server-side programming with ASP.NET. We expect that you’ll
already feel comfortable with HTML and a little CSS, as very little explanation of
these topics is provided here.
xxii
By the end of this book, you should be able to successfully download and install
Visual Web Developer 2008 Express Edition, and use it to create basic ASP.NET
pages. You’ll also learn how to install and run Microsoft SQL Server 2005 Express
Edition, create database tables, and work with advanced, dynamic ASP.NET pages
that query, insert, update, and delete information within a database.
All examples provided in the book are written in both Visual Basic and C#, the two
most popular languages for creating ASP.NET web sites. The examples start at be-
ginners’ level and proceed to more advanced levels. As such, no prior knowledge
of either language is required in order to read, understand, learn from, and apply
the knowledge provided in this book. Experience with other programming or
scripting languages (such as JavaScript) will certainly grease the wheels, though,
and should enable you to grasp fundamental programming concepts more quickly.
What’s in This Book?
This book comprises the following chapters. Read them from beginning to end to
gain a complete understanding of the subject, or skip around if you feel you need
a refresher on a particular topic.
Chapter 1: Introducing ASP.NET
Before you can start building your database-driven web presence, you must
ensure that you have the right tools for the job. In this first chapter, you’ll install
Visual Web Developer 2008 Express Edition and Microsoft SQL Server 2005
Express Edition. Finally, we’ll create a simple ASP.NET page to make sure that
everything’s running and properly configured.
Chapter 2: ASP.NET Basics
In this chapter, you’ll create your first useful ASP.NET page. We’ll explore all
of the components that make up a typical ASP.NET page, including directives,

controls, and code. Then, we’ll walk through the process of deployment, focusing
specifically on allowing the user to view the processing of a simple ASP.NET
page through a web browser.
Chapter 3: VB and C# Programming Basics
In this chapter, we’ll look at two of the programming languages that are used
to create ASP.NET pages: VB and C#. You’ll learn about the syntax of the two
languages as we explore the concepts of variables, data types, conditionals,
Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
xxiii
loops, arrays, functions, and more. Finally, we’ll see how the two languages
accommodate object oriented programming principles by allowing you to work
with classes, methods, properties, inheritance, and so on.
Chapter 4: Constructing ASP.NET Web Pages
ASP.NET web pages are known as web forms, but, as we’ll see, the process of
building ASP.NET web forms is a lot like composing a castle with Lego bricks!
ASP.NET is bundled with hundreds of controls—including HTML controls,
web controls, and so on—that are designed for easy deployment within your
applications. This chapter will introduce you to these building blocks and show
how to lock them together. You’ll also learn about master pages, which are a
very exciting feature of ASP.NET.
Chapter 5: Building Web Applications
A web application is basically a group of web forms, controls, and other elements
that work together to achieve complex functionality. So it’s no surprise that
when we build web applications, we must consider more aspects than when
we build individual web forms. This chapter touches on those aspects. We
configure your web application; learn how to use the application state, user
sessions, and cookies; explore the process for debugging errors in your project;
and more.
Chapter 6: Using the Validation Controls
This chapter introduces validation controls. With validation controls, Microsoft

basically eliminated the headache of fumbling through, and configuring, tired,
reused client-side validation scripts. First, we’ll learn how to implement user
input validation on both the client and server sides of your application using
Microsoft’s ready-made validation controls. Then, we’ll learn how to perform
more advanced validation using regular expressions and custom validators.
Chapter 7: Database Design and Development
Undoubtedly one of the most important chapters in the book, Chapter 7 will
prepare you to work with databases in ASP.NET. We’ll cover the essentials
you’ll need to know in order to create a database using SQL Server Express
Edition. Also in this chapter, we’ll begin to build the database for the Dorknozzle
intranet project.
Order the print version of this book to get all 700+ pages!

×