Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Tài liệu Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition docx
Nội dung xem thử
Mô tả chi tiết
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 C PANTONE Orange 021 C
CMYK 100, 45, 0, 37 CMYK O, 53, 100, 0
Black 100% Black 50%
#-9+
0ANTONE
'REYSCALE
PANTONE Orange 021 C PANTONE 2955 C
CMYK O, 53, 100, 0 CMYK 100, 45, 0, 37
Black 50% Black 100%
#-9+
0ANTONE
'REYSCALE
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 [email protected]
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 O
W
N
A
S
P.
N
ET 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 GUID
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: [email protected]
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 http://www.cristiandarie.ro.
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 http://tetlaw.id.au/.
About the Technical Director
As Technical Director for SitePoint, Kevin Yank oversees all of its technical publications—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 http://www.sitepoint.com/ to access our books, newsletters, articles, and
community forums.
1 http://www.sitepoint.com/blogs/category/net/
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!