Tải bản đầy đủ (.pdf) (1,009 trang)

Beginning ASP NET 4 in VB 2010

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 (21.17 MB, 1,009 trang )

  CYAN
  MAGENTA

  YELLOW
  BLACK
  PANTONE 123 C

BOOKS FOR PROFESSIONALS BY PROFESSIONALS ®

Beginning ASP.NET 4 in VB 2010
Dear Reader,

Welcome aboard!
Matthew MacDonald (Microsoft MVP, MCSD)

THE APRESS ROADMAP

Beginning
ASP.NET 4 in VB 2010

Companion eBook

Pro
ASP.NET 4 in VB 2010

Pro
ASP.NET MVC 2 Framework

Pro
VB 2010 and the
.NET 4 Platform



Pro
ASP.NET 4 CMS

Companion
eBook Available

Beginning

ASP.NET 4 in VB 2010

Matthew MacDonald,
Author of
Pro Silverlight 4 in VB
Pro WPF in VB 2010
Pro ASP.NET 4 in C# 2010
ASP.NET: The Complete
Reference

Welcome to the most up-to-date and comprehensive beginning ASP.NET book
you’ll find on any shelf. As you probably already know, ASP.NET is a framework
for developing modern web applications. In the right hands, ASP.NET produces web applications that are secure, blisteringly fast, and highly scalable. Best
of all, ASP.NET includes a huge set of ready-to-use features like website navigation, data binding, themes, and user management. ASP.NET allows you to
create everything from a dynamic personal website to a full-scale e-commerce
storefront.
In this book, I assume that you have only basic knowledge of VB, although
those coming from a more experienced background will find that the basics
are reviewed quickly and efficiently. As you explore ASP.NET, you’ll learn the
key database, security, and performance principles you need to know in order
to design a solid web application. My book will also teach you to use techniques such as object-oriented programming and code-behind development

from the beginning, rather than fake it with simplified techniques that won’t
work well in real life.
Once you’ve reached the end of the book, you will have mastered the core
techniques of website programming, and you’ll have the knowledge necessary to begin work as a professional ASP.NET developer.

THE EXPERT’S VOICE ® IN .NET

Beginning

ASP.NET 4
in VB 2010
Start your journey with the fundamentals
of building ASP.NET websites

See last page for details
on $10 eBook version

www.apress.com

ISBN 978-1-4302-2611-6
5 49 9 9

MacDonald

SOURCE CODE ONLINE

Matthew MacDonald

US $49.99
Shelve in

Programming Languages /
Visual Basic
User level:
Beginner–Intermediate

9 781430 226116

www.it-ebooks.info

this print for content only—size & color not accurate

spine = 1.875" 1008 page count


www.it-ebooks.info


Beginning ASP.NET 4
in VB 2010

■■■
Matthew MacDonald

www.it-ebooks.info


Beginning ASP.NET in VB 2010
Copyright © 2010 by Matthew MacDonald
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any
means, electronic or mechanical, including photocopying, recording, or by any information

storage or retrieval system, without the prior written permission of the copyright the publisher.
ISBN-13 (pbk): 978-1-4302-2611-6
ISBN-13 (electronic): 978-1-4302-2612-3
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names, logos, and images may appear in this book. Rather than use a trademark
symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and
images only in an editorial fashion and to the benefit of the trademark owner, with no intention of
infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if
they are not identified as such, is not to be taken as an expression of opinion as to whether or not
they are subject to proprietary rights.
President and Publisher: Paul Manning
Lead Editor: Ewan Buckingham
Technical Reviewer: Damien Foggon
Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell,
Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes,
Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic
Shakeshaft, Matt Wade, Tom Welsh
Coordinating Editor: Anne Collett
Copy Editor: Kim Wimpsett
Compositor: Mary Sudul
Indexer: BIM Indexing & Proofreading Services
Artist: April Milne
Cover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring
Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail , or visit www.springeronline.com.
For information on translations, please e-mail , or visit www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional
use. eBook versions and licenses are also available for most titles. For more information, reference
our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales.

The information in this book is distributed on an “as is” basis, without warranty. Although every
precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall
have any liability to any person or entity with respect to any loss or damage caused or alleged to be
caused directly or indirectly by the information contained in this work.

www.it-ebooks.info


For my wonderful family,
Faria, Maya, and Brenna

www.it-ebooks.info


www.it-ebooks.info


■ CONTENTS

Contents

About the Author .................................................................................................. xxv
About the Technical Reviewer ............................................................................. xxvi
Acknowledgments .............................................................................................. xxvii
Introduction ....................................................................................................... xxviii
Part 1: Introducing .NET ...........................................................................................1
■ Chapter 1: The .NET Framework ..........................................................................3
The Evolution of Web Development .................................................................................3
HTML and HTML Forms ..........................................................................................................................3
Server-Side Programming ......................................................................................................................6

Client-Side Programming .......................................................................................................................7

The .NET Framework .......................................................................................................9
VB, C#, and the .NET Languages ..........................................................................................................11

Intermediate Language..................................................................................................11
The Common Language Runtime .........................................................................................................13
The .NET Class Library..........................................................................................................................14
Visual Studio.........................................................................................................................................15

The Last Word................................................................................................................16
■ Chapter 2: The C# Language ..............................................................................17
The .NET Languages ......................................................................................................17
Variables and Data Types ..............................................................................................18
Assignment and Initializers ..................................................................................................................20

v

www.it-ebooks.info


■ CONTENTS

Arrays ...................................................................................................................................................21
Enumerations .......................................................................................................................................23

Variable Operations .......................................................................................................25
Advanced Math.....................................................................................................................................26
Type Conversions .................................................................................................................................27


Object-Based Manipulation ...........................................................................................28
The String Type ....................................................................................................................................29
The DateTime and TimeSpan Types .....................................................................................................31
The Array Type......................................................................................................................................33

Conditional Logic ...........................................................................................................35
The If . . . End If Block...........................................................................................................................35
The Select Case Block ..........................................................................................................................36

Loops .............................................................................................................................37
The For ... Next Block ...........................................................................................................................37
The For Each Block...............................................................................................................................38
The Do . . . Loop Block..........................................................................................................................39

Methods.........................................................................................................................40
Parameters ...........................................................................................................................................41
Method Overloading .............................................................................................................................41
Optional and Named Parameters..........................................................................................................42
Delegates..............................................................................................................................................43

The Last Word................................................................................................................45
■ Chapter 3: Types, Objects, and Namespaces .....................................................47
The Basics of Classes ....................................................................................................47
Shared and Instance Members.............................................................................................................48
A Simple Class......................................................................................................................................49

Building a Basic Class ...................................................................................................49
Creating an Object ................................................................................................................................51
Adding Properties .................................................................................................................................52
Automatic Properties ............................................................................................................................54


vi

www.it-ebooks.info


■ CONTENTS

Adding a Method ..................................................................................................................................54
Adding a Constructor............................................................................................................................55
Adding an Event....................................................................................................................................56
Testing the Product Class.....................................................................................................................59

Value Types and Reference Types.................................................................................61
Assignment Operations ........................................................................................................................61
Equality Testing ....................................................................................................................................62
Passing Parameters by Reference and by Value ..................................................................................62
Reviewing .NET Types ..........................................................................................................................63

Understanding Namespaces and Assemblies................................................................64
Using Namespaces ...............................................................................................................................66
Importing Namespaces.........................................................................................................................67
Assemblies ...........................................................................................................................................68

Advanced Class Programming.......................................................................................68
Inheritance............................................................................................................................................69
Shared Members ..................................................................................................................................70
Casting Objects.....................................................................................................................................71
Partial Classes ......................................................................................................................................73
Generics................................................................................................................................................75


The Last Word................................................................................................................76
Part 2: Developing ASP.NET Applications...............................................................77
■ Chapter 4: Visual Studio.....................................................................................79
The Promise of Visual Studio .........................................................................................79
Creating Websites..........................................................................................................80
Creating an Empty Web Application .....................................................................................................81
Websites and Web Projects ..................................................................................................................85
The Hidden Solution Files .....................................................................................................................86
The Solution Explorer ...........................................................................................................................87
Adding Web Forms ...............................................................................................................................88

vii

www.it-ebooks.info


■ CONTENTS

Migrating a Website from a Previous Version of Visual Studio.............................................................89

Designing a Web Page...................................................................................................91
Adding Web Controls ............................................................................................................................92
The Properties Window ........................................................................................................................94

The Anatomy of a Web Form .........................................................................................96
The Web Form Markup .........................................................................................................................96
The Page Directive................................................................................................................................97
The Doctype..........................................................................................................................................98
The Essentials of XHTML ....................................................................................................................100


Writing Code ................................................................................................................106
The Code-Behind Class ......................................................................................................................106
Adding Event Handlers .......................................................................................................................106
Outlining .............................................................................................................................................108
IntelliSense .........................................................................................................................................109
Code Formatting and Coloring ............................................................................................................114

Visual Studio Debugging..............................................................................................114
The Visual Studio Web Server ............................................................................................................115
Single-Step Debugging.......................................................................................................................116
Variable Watches................................................................................................................................121

The Last Word..............................................................................................................122
■ Chapter 5: Web Form Fundamentals................................................................123
The Anatomy of an ASP.NET Application .....................................................................123
ASP.NET File Types.............................................................................................................................124
ASP.NET Application Directories.........................................................................................................125

Introducing Server Controls .........................................................................................126
HTML Server Controls.........................................................................................................................127
Converting an HTML Page to an ASP.NET Page..................................................................................127
View State ..........................................................................................................................................130
The HTML Control Classes..................................................................................................................131

viii

www.it-ebooks.info



■ CONTENTS

Adding the Currency Converter Code .................................................................................................134
Behind the Scenes with the Currency Converter................................................................................137

Improving the Currency Converter...............................................................................139
Adding Multiple Currencies ................................................................................................................139
Storing Information in the List ............................................................................................................140
Adding Linked Images ........................................................................................................................142
Setting Styles .....................................................................................................................................144

A Deeper Look at HTML Control Classes......................................................................145
HTML Control Events ..........................................................................................................................145
Advanced Events with the HtmlInputImage Control ...........................................................................146
The HtmlControl Base Class ...............................................................................................................148
The HtmlContainerControl Class.........................................................................................................149
The HtmlInputControl Class ................................................................................................................149

The Page Class ............................................................................................................150
Sending the User to a New Page ........................................................................................................151
HTML Encoding...................................................................................................................................152

Application Events .......................................................................................................154
The global.asax File............................................................................................................................155
Additional Application Events .............................................................................................................156

ASP.NET Configuration ................................................................................................157
The web.config File ............................................................................................................................157
Nested Configuration ..........................................................................................................................158
Storing Custom Settings in the web.config File .................................................................................160

The Website Administration Tool (WAT) .............................................................................................162

The Last Word..............................................................................................................164
■ Chapter 6: Web Controls ..................................................................................167
Stepping Up to Web Controls .......................................................................................167
Basic Web Control Classes .................................................................................................................168
The Web Control Tags.........................................................................................................................169

ix

www.it-ebooks.info


■ CONTENTS

Web Control Classes . ..................................................................................................170
The WebControl Base Class..................................................................................................................171
Units . ....................................................................................................................................................173
Enumerations .......................................................................................................................................173
Colors . ..................................................................................................................................................174
Fonts. ....................................................................................................................................................175
Focus . ...................................................................................................................................................177
The Default Button................................................................................................................................177

List Controls. ...............................................................................................................178
Multiple-Select List Controls . ..............................................................................................................179
The BulletedList Control .......................................................................................................................181
Download from Wow! eBook <www.wowebook.com>

Table Controls. ............................................................................................................182

Web Control Events and AutoPostBack .......................................................................187
How Postback Events Work. ................................................................................................................191
The Page Life Cycle ..............................................................................................................................192

A Simple Web Page. ....................................................................................................195
Improving the Greeting Card Generator. ..............................................................................................200
Generating the Cards Automatically . ...................................................................................................202

The Last Word..............................................................................................................204
■ Chapter 7: Error Handling, Logging, and Tracing.............................................205
Common Errors............................................................................................................205
Exception Handling ......................................................................................................207
The Exception Class .............................................................................................................................207
The Exception Chain .............................................................................................................................209

Handling Exceptions . ..................................................................................................210
Catching Specific Exceptions . .............................................................................................................211
Nested Exception Handlers...................................................................................................................212
Exception Handling in Action................................................................................................................214
Mastering Exceptions ...........................................................................................................................215

x

www.it-ebooks.info


■ CONTENTS

Throwing Your Own Exceptions ...................................................................................216
Logging Exceptions......................................................................................................220

Viewing the Windows Event Logs.......................................................................................................220
Writing to the Event Log .....................................................................................................................223
Custom Logs.......................................................................................................................................225
A Custom Logging Class .....................................................................................................................227
Retrieving Log Information .................................................................................................................228

Page Tracing................................................................................................................230
Enabling Tracing.................................................................................................................................231
Tracing Information ............................................................................................................................231
Writing Trace Information...................................................................................................................237
Application-Level Tracing...................................................................................................................241

The Last Word..............................................................................................................243
■ Chapter 8: State Management .........................................................................245
The Problem of State ...................................................................................................245
View State....................................................................................................................245
The ViewState Collection....................................................................................................................246
A View State Example.........................................................................................................................246
Making View State Secure .................................................................................................................247
Retaining Member Variables ..............................................................................................................249
Storing Custom Objects ......................................................................................................................251

Transferring Information Between Pages ....................................................................252
Cross-Page Posting ............................................................................................................................252
The Query String.................................................................................................................................258

Cookies ........................................................................................................................262
A Cookie Example ...............................................................................................................................263

Session State...............................................................................................................264

Session Tracking ................................................................................................................................265
Using Session State............................................................................................................................265

xi

www.it-ebooks.info


■ CONTENTS

A Session State Example....................................................................................................................267

Session State Configuration ........................................................................................269
Cookieless ..........................................................................................................................................270
Timeout...............................................................................................................................................273
Mode...................................................................................................................................................274

Application State..........................................................................................................278
An Overview of State Management Choices................................................................280
The Last Word..............................................................................................................282
Part 3: Building Better Web Forms .......................................................................283
■ Chapter 9: Validation .......................................................................................285
Understanding Validation.............................................................................................285
The Validation Controls.......................................................................................................................286
Server-Side Validation........................................................................................................................287
Client-Side Validation .........................................................................................................................287

The Validation Controls................................................................................................287
A Simple Validation Example ..............................................................................................................289
Other Display Options .........................................................................................................................291

Manual Validation ...............................................................................................................................294
Validating with Regular Expressions ..................................................................................................296
A Validated Customer Form ................................................................................................................300
Validation Groups................................................................................................................................304

The Last Word..............................................................................................................306
■ Chapter 10: Rich Controls ................................................................................307
The Calendar................................................................................................................307
Formatting the Calendar.....................................................................................................................309
Restricting Dates ................................................................................................................................311

The AdRotator ..............................................................................................................315
The Advertisement File.......................................................................................................................315
The AdRotator Class ...........................................................................................................................317

xii

www.it-ebooks.info


■ CONTENTS

Pages with Multiple Views...........................................................................................318
The MultiView Control ........................................................................................................................320
The Wizard Control .............................................................................................................................325

The Last Word..............................................................................................................333
■ Chapter 11: User Controls and Graphics ..........................................................335
User Controls ...............................................................................................................335
Creating a Simple User Control ..........................................................................................................336

Independent User Controls .................................................................................................................339
Integrated User Controls.....................................................................................................................341
User Control Events ............................................................................................................................344
Passing Information with Events ........................................................................................................346

Dynamic Graphics........................................................................................................349
Basic Drawing ....................................................................................................................................349
Drawing a Custom Image ...................................................................................................................352
Placing Custom Images Inside Web Pages.........................................................................................353
Image Format and Quality ..................................................................................................................355

The Last Word..............................................................................................................357
■ Chapter 12: Styles, Themes, and Master Pages...............................................359
Styles ...........................................................................................................................359
Style Types .........................................................................................................................................360
Creating a Basic Inline Style...............................................................................................................360
Creating a Style Sheet ........................................................................................................................369
Applying Style Sheet Rules.................................................................................................................372

Themes ........................................................................................................................375
How Themes Work .............................................................................................................................375
Applying a Simple Theme...................................................................................................................377
Handling Theme Conflicts...................................................................................................................378
Creating Multiple Skins for the Same Control ....................................................................................380
More Advanced Skins.........................................................................................................................381

xiii

www.it-ebooks.info



■ CONTENTS

Master Page Basics .....................................................................................................383
A Simple Master Page and Content Page ...........................................................................................384
How Master Pages and Content Pages Are Connected ......................................................................388
A Master Page with Multiple Content Regions ...................................................................................390
Default Content...................................................................................................................................393
Master Pages and Relative Paths .......................................................................................................394

Advanced Master Pages ..............................................................................................395
Style-Based Layouts...........................................................................................................................395
Code in a Master Page........................................................................................................................400
Interacting with a Master Page Programmatically .............................................................................401

The Last Word..............................................................................................................402
■ Chapter 13: Website Navigation.......................................................................403
Site Maps.....................................................................................................................403
Defining a Site Map ............................................................................................................................404
Seeing a Simple Site Map in Action....................................................................................................408
Binding an Ordinary Page to a Site Map.............................................................................................409
Binding a Master Page to a Site Map .................................................................................................410
Binding Portions of a Site Map ...........................................................................................................412
The SiteMap Class ..............................................................................................................................417

URL Mapping and Routing ...........................................................................................419
URL Mapping ......................................................................................................................................420
URL Routing ........................................................................................................................................420

The SiteMapPath Control .............................................................................................422

Customizing the SiteMapPath ............................................................................................................423
Using SiteMapPath Styles and Templates ..........................................................................................424
Adding Custom Site Map Information.................................................................................................426

The TreeView Control...................................................................................................427
TreeView Properties ...........................................................................................................................428
TreeView Styles ..................................................................................................................................431

xiv

www.it-ebooks.info


■ CONTENTS

The Menu Control ........................................................................................................435
Menu Styles........................................................................................................................................437
Menu Templates .................................................................................................................................438

The Last Word..............................................................................................................441
Part 4: Working with Data ....................................................................................443
■ Chapter 14: ADO.NET Fundamentals ................................................................445
Understanding Databases............................................................................................445
Configuring Your Database ..........................................................................................447
SQL Server Express ............................................................................................................................447
Browsing and Modifying Databases in Visual Studio .........................................................................448
The sqlcmd Command-Line Tool ........................................................................................................451

SQL Basics...................................................................................................................452
Running Queries in Visual Studio .......................................................................................................453

The Select Statement .........................................................................................................................455
The SQL Update Statement.................................................................................................................457
The SQL Insert Statement...................................................................................................................459
The SQL Delete Statement..................................................................................................................459

The Data Provider Model .............................................................................................460
Direct Data Access ......................................................................................................461
Creating a Connection ........................................................................................................................463
The Select Command .........................................................................................................................469
The DataReader ..................................................................................................................................470
Putting It All Together.........................................................................................................................470
Updating Data .....................................................................................................................................475

Disconnected Data Access ..........................................................................................485
Selecting Disconnected Data..............................................................................................................486
Selecting Multiple Tables ...................................................................................................................487
Defining Relationships........................................................................................................................489

The Last Word..............................................................................................................492

xv

www.it-ebooks.info


■ CONTENTS

■ Chapter 15: Data Binding .................................................................................493
Introducing Data Binding .............................................................................................493
Types of ASP.NET Data Binding..........................................................................................................494

How Data Binding Works ....................................................................................................................494

Single-Value Data Binding ...........................................................................................495
A Simple Data Binding Example .........................................................................................................495
Simple Data Binding with Properties..................................................................................................498
Problems with Single-Value Data Binding..........................................................................................500
Using Code Instead of Simple Data Binding .......................................................................................500

Repeated-Value Data Binding......................................................................................501
Data Binding with Simple List Controls ..............................................................................................501
A Simple List Binding Example...........................................................................................................502
Strongly Typed Collections .................................................................................................................503
Multiple Binding..................................................................................................................................504
Data Binding with a Dictionary Collection ..........................................................................................506
Using the DataValueField Property .....................................................................................................507
Data Binding with ADO.NET ................................................................................................................508
Creating a Record Editor.....................................................................................................................510

Data Source Controls ...................................................................................................515
The Page Life Cycle with Data Binding...............................................................................................517
The SqlDataSource .............................................................................................................................518
Selecting Records...............................................................................................................................519
Parameterized Commands .................................................................................................................521
Handling Errors...................................................................................................................................527
Updating Records ...............................................................................................................................528

The Last Word..............................................................................................................532
■ Chapter 16: The Data Controls .........................................................................533
The GridView................................................................................................................533
Automatically Generating Columns ....................................................................................................534


xvi

www.it-ebooks.info


■ CONTENTS

Defining Columns ...............................................................................................................................536

Formatting the GridView ..............................................................................................540
Formatting Fields................................................................................................................................540
Using Styles........................................................................................................................................541
Formatting-Specific Values ................................................................................................................544

Selecting a GridView Row............................................................................................546
Adding a Select Button .......................................................................................................................547
Using Selection to Create Master-Details Pages ................................................................................548

Editing with the GridView ............................................................................................550
Sorting and Paging the GridView .................................................................................553
Sorting ................................................................................................................................................553
Paging.................................................................................................................................................555

Using GridView Templates...........................................................................................557
Using Multiple Templates ...................................................................................................................559
Editing Templates in Visual Studio .....................................................................................................560
Handling Events in a Template ...........................................................................................................561
Editing with a Template......................................................................................................................562


The DetailsView and FormView ...................................................................................567
The DetailsView ..................................................................................................................................567
The FormView.....................................................................................................................................569

The Last Word..............................................................................................................572
■ Chapter 17: Files and Streams.........................................................................573
Files and Web Applications..........................................................................................573
File System Information...............................................................................................574
The Path Class ....................................................................................................................................575
The Directory and File Classes ...........................................................................................................576
The DirectoryInfo and FileInfo Classes ...............................................................................................581
The DriveInfo Class.............................................................................................................................583
A Sample File Browser .......................................................................................................................584

xvii

www.it-ebooks.info


■ CONTENTS

Reading and Writing with Streams ..............................................................................588
Text Files ............................................................................................................................................588
Binary Files.........................................................................................................................................590
Shortcuts for Reading and Writing Files.............................................................................................591
A Simple Guest Book ..........................................................................................................................592

Allowing File Uploads ..................................................................................................598
The FileUpload Control .......................................................................................................................598


The Last Word..............................................................................................................601
■ Chapter 18: XML...............................................................................................603
XML Explained .............................................................................................................603
Improving the List with XML...............................................................................................................605
XML Basics .........................................................................................................................................606
Attributes ............................................................................................................................................607
Comments ..........................................................................................................................................608

The XML Classes..........................................................................................................608
The XML TextWriter ............................................................................................................................609
The XML Text Reader..........................................................................................................................612
Working with XML Documents in Memory .........................................................................................616
Reading an XML Document ................................................................................................................621
Searching an XML Document .............................................................................................................623

XML Validation .............................................................................................................624
XML Namespaces ...............................................................................................................................625
XML Schema Definition ......................................................................................................................628
Validating an XML Document..............................................................................................................629

XML Display and Transforms .......................................................................................632
The Xml Web Control ..........................................................................................................................635

The Last Word..............................................................................................................636

xviii

www.it-ebooks.info



■ CONTENTS

Part 5: Website Security .......................................................................................637
■ Chapter 19: Security Fundamentals.................................................................639
Understanding Security ...............................................................................................639
Testing and Deploying Security Settings............................................................................................640

Authentication and Authorization ................................................................................641
Forms Authentication ..................................................................................................641
Web.config Settings ...........................................................................................................................643
Authorization Rules ............................................................................................................................643
The WAT .............................................................................................................................................647
The Login Page ...................................................................................................................................649

Windows Authentication ..............................................................................................655
Web.config Settings ...........................................................................................................................655
A Windows Authentication Test..........................................................................................................658

The Last Word..............................................................................................................659
■ Chapter 20: Membership..................................................................................661
The Membership Data Store ........................................................................................661
Membership with SQL Server Express ...............................................................................................663
Using the Full Version of SQL Server ..................................................................................................665
Configuring the Membership Provider................................................................................................668
Creating Users with the WAT..............................................................................................................671
The Membership and MembershipUser Classes ................................................................................674
Authentication with Membership .......................................................................................................678
Disabled Accounts ..............................................................................................................................679

The Security Controls ..................................................................................................679

The Login Control................................................................................................................................680
The CreateUserWizard Control............................................................................................................686
The PasswordRecovery Control..........................................................................................................690

Role-Based Security ....................................................................................................693
Creating and Assigning Roles.............................................................................................................693

xix

www.it-ebooks.info


■ CONTENTS

Restricting Access Based on Roles . ....................................................................................................697
The LoginView Control..........................................................................................................................697

The Last Word..............................................................................................................699
■ Chapter 21: Profiles .........................................................................................701
Understanding Profiles . ..............................................................................................701
Profile Performance..............................................................................................................................702
How Profiles Store Data........................................................................................................................702

Using the SqlProfileProvider . ......................................................................................704
Enabling Authentication........................................................................................................................704
Using SQL Server Express ....................................................................................................................705
Download from Wow! eBook <www.wowebook.com>

Using the Full Version of SQL Server . ..................................................................................................705
The Profile Databases...........................................................................................................................707

Defining Profile Properties....................................................................................................................708
Using Profile Properties ........................................................................................................................709
Profile Serialization ..............................................................................................................................711
Profile Groups .......................................................................................................................................713
Profiles and Custom Data Types. .........................................................................................................714
The Profile API ......................................................................................................................................717
Anonymous Profiles..............................................................................................................................720

The Last Word..............................................................................................................723
Part 6: Advanced ASP.NET....................................................................................725
■ Chapter 22: Component-Based Programming .................................................727
Why Use Components? ................................................................................................727
Component Jargon . ....................................................................................................728
Three-Tier Design .................................................................................................................................728
Encapsulation .......................................................................................................................................730
Business Objects ..................................................................................................................................730
Data Objects .........................................................................................................................................730
Components and Classes .....................................................................................................................730

xx

www.it-ebooks.info


■ CONTENTS

Creating a Component .................................................................................................731
Classes and Namespaces...................................................................................................................732
Class Members ...................................................................................................................................733
Adding a Reference to the Component...............................................................................................734

Using the Component .........................................................................................................................736

Properties and State ....................................................................................................738
A Stateful Account Class ....................................................................................................................739
A Stateless AccountUtility Class .........................................................................................................740

Data-Access Components............................................................................................741
A Simple Data-Access Component .....................................................................................................741
Using the Data-Access Component ....................................................................................................745
Enhancing the Component with Error Handling..................................................................................748
Enhancing the Component with Aggregate Information.....................................................................749

The ObjectDataSource .................................................................................................750
Making Classes the ObjectDataSource Can Understand ....................................................................750
Selecting Records...............................................................................................................................751
Using Method Parameters ..................................................................................................................751
Updating Records ...............................................................................................................................752

The Last Word..............................................................................................................755
■ Chapter 23: Caching.........................................................................................757
Understanding Caching ...............................................................................................757
When to Use Caching..........................................................................................................................758
Caching in ASP.NET ............................................................................................................................759

Output Caching ............................................................................................................759
Caching on the Client Side .................................................................................................................761
Caching and the Query String.............................................................................................................761
Caching with Specific Query String Parameters.................................................................................762
A Multiple Caching Example ...............................................................................................................763
Fragment Caching ..............................................................................................................................765

Cache Profiles.....................................................................................................................................765

xxi

www.it-ebooks.info


■ CONTENTS

Data Caching ...............................................................................................................766
Adding Items to the Cache .................................................................................................................766
A Simple Cache Test...........................................................................................................................768
Caching to Provide Multiple Views .....................................................................................................769
Caching with the Data Source Controls..............................................................................................772

Caching with Dependencies ........................................................................................777
File Dependencies ..............................................................................................................................777
Cache Item Dependencies ..................................................................................................................778
SQL Server Cache Dependencies .......................................................................................................779

The Last Word..............................................................................................................782
■ Chapter 24: LINQ and the Entity Framework....................................................783
Understanding LINQ.....................................................................................................783
LINQ Basics..................................................................................................................784
LINQ Expressions................................................................................................................................787

The Entity Framework..................................................................................................791
Creating an Entity Data Model ............................................................................................................792
The Data Model Diagram ....................................................................................................................795
Updating a Data Model .......................................................................................................................798

The Data Model Code .........................................................................................................................800
Querying the Data Model ....................................................................................................................801
Handling Errors...................................................................................................................................802
Navigating Relationships ....................................................................................................................804

Getting More Advanced with the Entity Framework ....................................................807
Querying with LINQ to Entities............................................................................................................807
Controlling When Data is Loaded........................................................................................................810
Updates, Inserts, and Deletes.............................................................................................................811
Managing Concurrency.......................................................................................................................814

The EntityDataSource ..................................................................................................815
Displaying Data...................................................................................................................................815

xxii

www.it-ebooks.info


■ CONTENTS

Editing Data ........................................................................................................................................820

The Last Word..............................................................................................................820
■ Chapter 25: ASP.NET AJAX...............................................................................821
Understanding Ajax......................................................................................................821
Ajax: The Good....................................................................................................................................822
Ajax: The Bad......................................................................................................................................822
The ASP.NET AJAX Toolkit ..................................................................................................................823
The ScriptManager .............................................................................................................................823


Partial Refreshes .........................................................................................................825
A Simple UpdatePanel Test ................................................................................................................826
Handling Errors...................................................................................................................................829
Conditional Updates............................................................................................................................831
Triggers ..............................................................................................................................................832

Progress Notification ...................................................................................................837
Showing a Simulated Progress Bar ....................................................................................................837
Cancellation........................................................................................................................................839

Timed Refreshes..........................................................................................................841
The ASP.NET AJAX Control Toolkit...............................................................................843
Installing the ASP.NET AJAX Control Toolkit.......................................................................................843
The Accordion.....................................................................................................................................846
The AutoCompleteExtender ................................................................................................................849
Getting More Controls.........................................................................................................................852

The Last Word..............................................................................................................857
■ Chapter 26: Deploying ASP.NET Applications ..................................................859
ASP.NET Applications and the Web Server..................................................................859
How Web Servers Work ......................................................................................................................859
The Virtual Directory...........................................................................................................................861
Web Application URLs.........................................................................................................................861
Web Farms .........................................................................................................................................863

xxiii

www.it-ebooks.info



×