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

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

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

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

—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

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!

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!

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!

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

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

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,

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!


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status