Tải bản đầy đủ

Build your own ASP NET 4 web site using c VB, 4th edition

Pantone:

CMYK:

BUILD YOUR OWN

ASP.NET 4
WEBSITE
Grey scale

PANTONE Orange 021 C

PANTONE 2955 C

CMYK O, 53, 100, 0

CMYK 100, 45, 0, 37

Black 50%

Black 100%


USING C# & VB
BY CRISTIAN DARIE
WYATT BARNETT
& TIM POSEY
4TH EDITION

THE ULTIMATE ASP.NET BEGINNER’S GUIDE
www.it-ebooks.info


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

www.it-ebooks.info


www.it-ebooks.info



BUILD YOUR OWN
ASP.NET 4 WEBSITE
USING C# & VB
BY CRISTIAN DARIE
WYATT BARNETT
TIM POSEY
4TH EDITION

www.it-ebooks.info


iv

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

Printing History:

Latest Update: Fourth Edition: September

First Edition: April 2004
Second Edition: October 2006
Third Edition: September 2008

2011

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

www.it-ebooks.info


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. 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.

www.it-ebooks.info


www.it-ebooks.info


To my family and friends.
—Cristian Darie

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

For LJ and Erin.
—Tim Posey

www.it-ebooks.info


www.it-ebooks.info


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

www.it-ebooks.info


x

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

www.it-ebooks.info


xi
Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Understanding Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Objects in .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Code-behind Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

82
83
84
84
85
87
88
94

Constructing ASP.NET Web
Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

97

Chapter 4

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

www.it-ebooks.info


xii
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 . . . . . . . . . . . . . . . . .
Introducing the ASP.NET Validation Controls . . . . . . . . . . . . . . . . . . . . .
Enforcing Validation on the Server . . . . . . . . . . . . . . . . . . . . . . . . .
Using Validation Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
RequiredFieldValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
CompareValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
RangeValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ValidationSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

www.it-ebooks.info

236
236
240
246
247
248
251
252


xiii
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

www.it-ebooks.info


xiv
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

www.it-ebooks.info


xv

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

www.it-ebooks.info


xvi

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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Updating DetailsView Records . . . . . . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Chapter 12

492
496
500
505

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

www.it-ebooks.info


xvii

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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Setting Up Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Writing Content to a Text File . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Reading Content from a Text File . . . . . . . . . . . . . . . . . . . . . . . . . .
Accessing Directories and Directory Information . . . . . . . . . . . . . . . . . .
Working with Directory and File Paths . . . . . . . . . . . . . . . . . . . . . .
Uploading Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Sending Email with ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Sending a Test Email . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating the Company Newsletters Page . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

www.it-ebooks.info

616
617
620
624
628
632
635
639
641
643
653


xviii

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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Standard Web Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

www.it-ebooks.info

723
723
724
725


xix
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

www.it-ebooks.info


xx
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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

www.it-ebooks.info

775


Foreword
Before you go much further in reading this book, give yourself a small pat on the
back for investing the money, time and effort in learning ASP.NET. Perhaps it is a
new technology to you, or perhaps you are familiar with ASP or other programming
in .NET. Either way, it’s a great skill to add to your toolbox and increase your value
as a developer.
ASP.NET is useful in more ways than one. If you aren’t already a .NET developer,
it’s the gateway to learning the framework, and the languages that you can use to
program against it. The most common languages, and the ones covered in this book,
are C# and VB.NET. Skills in these languages and framework go way beyond web
development. You can use them for mobile development with Silverlight, which
uses the .NET framework for Windows Phone 7 Desktop development; or .NET on
Windows Power Desktop development with the Windows Presentation Foundation
(WPF), part of the .NET Framework Workflow development for business processes
using the Workflow Foundation (WF)—which is also part of the .NET Framework
Connected systems development using the Windows Communication Foundation
(WCF).
Beyond these, the skills continue to grow in relevance as the industry matures and
develops. Time invested in .NET development will reap benefits with cloud-scalable
applications using Windows Azure, as well as the new Windows 8 client applications. But you have to start somewhere, and starting with the web is a wise choice.
ASP.NET allows you to build dynamic websites, web applications and web services.
As a developer, you know and understand that there as many different types of web
application as there are web applications themselves, and you need a powerful and
flexible framework that will allow you to build them, without having to reinvent
the wheel each time.
ASP.NET is this framework, and with its Web Forms and Controls technologies,
you can use rapid development methodologies to get your application up and running quickly. Being fully standards-compliant, you can also make it beautiful using
CSS. Beyond this, particularly for professional, commercial applications, you’ll
need tools that allow database connectivity to be smart, secure, and efficient, and
ASP.NET with its ADO.NET technology provides this for you.

www.it-ebooks.info


xxii
And of course it wouldn’t be Web 2.0 if you didn’t have the ability to use Ajax.
ASP.NET gives you simple but effective ways to use AJAX with server-side controls
that do a lot of the hard work of handling asynchronous page updates for you. Indeed,
server-side coding is something that you’ll do a lot of with ASP.NET. It’s amazing
how simple it can make writing distributed applications, where the server is smart
enough to manage sessions, connectivity, presentation and more on your behalf.
This book provides you with everything you need to know to skill up in ASP.NET
development with Web Forms technology. It’s a fantastic learning tool, written in
an approachable and informative way. I strongly recommend you pick up your copy
of this book, download the free Visual Web Developer Express tools, and start coding
in ASP.NET. You’ll be amazed at what you can build, quickly and easily.
Laurence Moroney, technologist and author
August 2011

www.it-ebooks.info


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 development. 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. Finally, we’ll
stretch your abilities by introducing the MVC Framework and other advanced topics.
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 project—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, CSS, and a little knowlegable about database

www.it-ebooks.info


xxiv
design although we will cover quite a few databatse topics along the way. Developers
in open-source web development languages such as PHP, Java, or Ruby will make
an excellent transition to learning ASP.NET.
By the end of this book, you should be able to successfully download and install
Visual Web Developer 2010 Express Edition, and use it to create basic ASP.NET
pages. You’ll also learn how to install and run Microsoft SQL Server 2008 R2 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 websites. The examples start at beginners’ 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 2010 Express Edition and Microsoft SQL Server 2008 R2
Express Edition. Finally, you’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
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.

www.it-ebooks.info


Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay

×