652 Professional ASP.NET 4 in C# and VB
™ Programmer to Programmer
Get more out of Interact Join the Community Take an active role online by participating in our Sign up for our free monthly newsletter at P2P forums @ p2p.wrox.com newsletter.wrox.com Wrox Online Library Browse Hundreds of our books are available online Ready for more Wrox? We have books and through Books24x7.com e-books available on .NET, SQL Server, Java,
XML, Visual Basic, C#/ C++, and much more! Wrox Blox Download short informational pieces and code to keep you up to date and out of trouble!
Professional asP.neT 4
inTroducTion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxix
chaPTer 14 Personalization � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �569
chaPTer 25 User and Server Controls � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 979
chaPTer 24 File I/O and Streams � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 927
chaPTer 23 Debugging and Error Handling � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �895
chaPTer 22 Caching � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �865
chaPTer 21 State Management � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �835
chaPTer 20 Security � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �805
chaPTer 19 ASP�NET AJAX Control Toolkit � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 749
chaPTer 18 ASP�NET AJAX � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 709
chaPTer 17 HTML and CSS Design with ASP�NET � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �683
chaPTer 16 Portal Frameworks and Web Parts � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �643
chaPTer 15 Membership and Role Management � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 597
chaPTer 13 Site Navigation � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 519
chaPTer 1 Application and Page Frameworks � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �1
chaPTer 12 Extending the Provider Model � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 491
chaPTer 11 Introduction to the Provider Model � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 457
chaPTer 10 Working with XML and LINQ to XML � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �405
chaPTer 9 Querying with LINQ � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 371
chaPTer 8 Data Management with ADO�NET � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �309
chaPTer 7 Data Binding � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 237
chaPTer 6 Themes and Skins � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 217
chaPTer 5 Working with Master Pages � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 187
chaPTer 4 Validation Server Controls � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 157
chaPTer 3 ASP�NET Web Server Controls � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 85
chaPTer 2 ASP�NET Server Controls and Client-Side Scripts � � � � � � � � � � � � � � � � � � � � � � � � � � � � 49
chaPTer 26 Modules and Handlers � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 1045
chaPTer 27 ASP�NET MVC � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 1059
chaPTer 28 Using Business Objects � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 1081
chaPTer 29 ADO�NET Entity Framework � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 1105
chaPTer 30 ASP�NET Dynamic Data � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 1129
chaPTer 31 Working with Services � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 1153
chaPTer 32 Building Global Applications � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �1217
chaPTer 33 Configuration � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 1239
chaPTer 34 Instrumentation � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 1285
chaPTer 35 Administration and Management � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 1315
chaPTer 36 Packaging and Deploying ASP�NET Applications � � � � � � � � � � � � � � � � � � � � � � � � � � 1337 aPPendix a Migrating Older ASP�NET Projects � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 1369 aPPendix B ASP�NET Ultimate Tools � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 1381 aPPendix c Silverlight 3 and ASP�NET � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 1399 aPPendix d Dynamic Types and Languages � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 1419 aPPendix e ASP�NET Online Resources � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � 1427
index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1429
Professional
asP.neT 4
Professional
asP.neT 4
in C# and VB
Bill Evjen
Scott Hanselman
Devin Rader
Professional asP.neT 4: in C# and VB
Published by Wiley Publishing, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 Copyright © 2010 by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada
ISBN: 978-0-470-50220-4 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with
respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read.
For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books.
Library of Congress Control Number: 2009943645 Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are
trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book.
To Tuija, always.
—Bill Evjen To Momo and the boys. Toot!
—Scott Hanselman
aBouT The auThors Bill evjen is an active proponent of .NET technologies and community-based learning
initiatives for .NET. He has been actively involved with .NET since the first bits were released in 2000. In the same year, Bill founded the St. Louis .NET User Group (www.stlnet.org), one of the world’s first such groups. Bill is also the founder and former executive director of the International .NET Association (www.ineta.org), which represents more than 500,000 members worldwide. Based in St. Louis, Missouri, Bill is an acclaimed author and speaker on ASP.NET and Services. He has authored or coauthored more than 20 books including Professional C# 2010, Professional VB 2008,
ASP.NET Professional Secrets, XML Web Services for ASP.NET, and Web Services Enhancements:
Understanding the WSE for Enterprise Applications (all published by Wiley). In addition to writing, Bill is a
speaker at numerous conferences, including DevConnections, VSLive!, and TechEd. Along with these items, Bill works closely with Microsoft as a Microsoft Regional Director and an MVP. Bill is the Global Head of Platform Architecture for Thomson Reuters, Lipper, the international news and financial services company (www.thomsonreuters.com). He graduated from Western Washington University in Bellingham, Washington, with a Russian language degree. When he isn’t tinkering on the computer, he can usually be found at his summer house in Toivakka, Finland. You can reach Bill on Twitter at @billevjen.
scoTT hanselman works for Microsoft as a Principal Program Manager Lead in the
Server and Tools Online Group, aiming to spread the good word about developing software, most often on the Microsoft stack. Before this, Scott was the Chief Architect at Corillian, an eFinance enabler, for 6+ years, and before Corillian, he was a Principal Consultant at Microsoft Gold Partner for 7 years. He was also involved in a few things like the MVP and RD programs and will speak about computers (and other passions) whenever someone will listen to him. He blogs at www.hanselman.com, podcasts at www.hanselminutes.com, and runs a team that contributes to www.asp.net, www.windowsclient.net, and www.silverlight.net. Follow Scott on Twitter @shanselman.
devin rader works at Infragistics where he focuses on delivering great experiences to
developers using their controls. He’s done work on all of the .NET platforms, but most recently has been focused on Web technologies ASP.NET and Silverlight. As a co-founder of the St. Louis .NET User group and a former INETA board member, and a member of the Central New Jersey .NET user group, he’s an active supporter of the .NET developer community. He’s also co-author or technical editor of numerous books on .NET, including Wrox’s Silverlight 3 Programmer’s Reference. Follow Devin on Twitter @devinrader.
aBouT The Technical ediTors
carlos figueroa has been developing and designing Web solutions for the last 8 years, participating
in international projects for the pharmaceutical industry, banking, commercial air transportation, and the government. During these years, Carlos has been deeply involved as an early adopter of Microsoft Web development technologies, such as ASP.NET and Silverlight. He has been awarded Microsoft Most Valuable Professional for the last 5 years and holds the MCAD certification. Carlos is a Senior Software Developer at Oshyn, Inc. (www.oshyn.com), a company specialized on delivering innovative business solutions for the web, mobile devices and emerging technology platforms. At Oshyn, Carlos is dedicated to help some of the most recognizable brands in the world to achieve technology success. You can reach Carlos at cfigueroa1982@hotmail.com or follow him on twitter @carlosfigueroa.
andrew moore is a graduate of Purdue University–Calumet in Hammond, Indiana, and has been
developing software since 1998 for radar systems, air traffic management, discrete-event simulation, and business communications applications using C, C++, C#, and Java on the Windows, UNIX, and Linux platforms. Andrew is also a contributor to the Wrox Blox article series. He is currently working as a Senior Software Engineer at Interactive Intelligence, Inc., in Indianapolis, Indiana, developing server-side applications for a multimedia unified business communications platform. Andrew lives in Indiana with his wife Barbara and children Sophia and Andrew.
crediTs acquisiTions ediTor Paul Reese senior ProjecT ediTor Kevin Kent Technical ediTors Carlos Figueroa Andrew Moore ProducTion ediTor Daniel Scribner coPy ediTor Paula Lowell ediTorial direcTor Robyn B� Siesky ediTorial manager Mary Beth Wakefield associaTe direcTor of markeTing David Mayhew ProducTion manager Tim Tate vice PresidenT and execuTive grouP PuBlisher Richard Swadley vice PresidenT and execuTive PuBlisher Barry Pruett associaTe PuBlisher Jim Minatel ProjecT coordinaTor, cover Lynsey Stanford Proofreaders Word One indexer J & J Indexing cover designer Michael E� Trent cover image © Jon Feingersh Photography Inc / Blend Images / Jupiter Images
acknowledgmenTs
Thanks To kevin kenT, Paul reese, and jim minaTel for the opportunity to work on such a great
book. In addition to my co-authors, I would like to thank my family for putting up with all the writing.Thank you Tuija, Sofia, Henri, and Kalle!
—Bill Evjen
conTenTs
INTRODUCTION xxxix
chaPTer 1: aPPlicaTion and Page frameworks
1 application location options
1 Built-in Web Server
2 IIS
3 FTP
3 Web Site Requiring FrontPage Extensions
4 The asP.neT Page structure options
4 Inline Coding
6 Code-Behind Model
7 asP.neT 4 Page directives
9 @Page
10 @Master 13 @Control 14 @Import 15 @Implements 17 @Register 17 @Assembly 18 @PreviousPageType 18 @MasterType 18 @OutputCache 18 @Reference
19 asP.neT Page events
19 dealing with Postbacks
20 Cross-Page Posting 21 asP.neT application folders
26 App_Code Folder
27 App_Data Folder
30 App_Themes Folder
31 App_GlobalResources Folder
31 App_LocalResources Folder
31 App_WebReferences Folder
31 App_Browsers Folder
31 Compilation
32 Build Providers
35
conTenTs Using the Built-in Build Providers
36 Using Your Own Build Providers
36 Global.asax
41 Working with Classes Through Visual studio 2010
44 summary
47
chaPTer 2: asP.neT server conTrols and clienT-side scriPTs 49 asP.neT server Controls
49 Types of Server Controls
50 Building with Server Controls
51 Working with Server Control Events
53 applying styles to server Controls
54 Examining the Controls’ Common Properties
54 Changing Styles Using Cascading Style Sheets
56 CSS Changes in ASP�NET 4
58 HTMl server Controls
59 Looking at the HtmlControl Base Class
60 Looking at the HtmlContainerControl Class
61 Looking at All the HTML Classes
61 Using the HtmlGenericControl Class
62 identifying asP.neT server Controls
63 Manipulating Pages and server Controls with Javascript
65 Using Page�ClientScript�RegisterClientScriptBlock
66 Using Page�ClientScript�RegisterStartupScript
68 Using Page�ClientScript�RegisterClientScriptInclude
69 Client-side Callback
69 Comparing a Typical Postback to a Callback
70 Using the Callback Feature — A Simple Approach
72 Using the Callback Feature with a Single Parameter
75 Using the Callback Feature — A More Complex Example
78 summary
83
chaPTer 3: asP.neT weB server conTrols
85 an overview of Web server Controls
85 The label server Control
86 The literal server Control
88 The TextBox server Control
88 Using the Focus() Method
89 Using AutoPostBack
89 Using AutoCompleteType
90 xvi
xvii ConTenTs The Button server Control
102 The CheckBox server Control 102
122 The PlaceHolder server Control 124 Bulletedlist server Control 124 Hiddenfield server Control 129 fileUpload server Control 130
119 The Xml server Control 121 Panel server Control
115 Modifying the Style and Behavior of Your Calendar 116 adrotator server Control
Making a Date Selection from the Calendar Control 112 Choosing a Date Format to Output from the Calendar 113 Making Day, Week, or Month Selections 114 Working with Date Ranges
110 The Calendar server Control 112
The CheckBoxlist server Control 104 The radioButton server Control 106 The radioButtonlist server Control 108 image server Control 109 Table server Control
How to Determine Whether Check Boxes Are Checked 103 Assigning a Value to a Check Box 104 Aligning Text Around the Check Box 104
Allowing Users to Select Multiple Items 100 An Example of Using the ListBox Control 100 Adding Items to a Collection
91 The CausesValidation Property
98 The listBox server Control 100
96 Visually removing items from a Collection
96 The dropdownlist server Control
95 The Hyperlink server Control
94 The imageButton server Control
93 The linkButton server Control
92 Buttons That Work with Client-Side JavaScript
91 The CommandName Property
Uploading Files Using the FileUpload Control 130 Giving ASP�NET Proper Permissions to Upload Files 132 Understanding File Size Limitations 133 Uploading Multiple Files from the Same Page 134 Placing the Uploaded File into a Stream Object 137 Moving File Contents from a Stream Object to a Byte Array 137
xviii conTenTs MultiView and View server Controls 138 Wizard server Control 141
Customizing the Side Navigation 142 Examining the AllowReturn Attribute 143 Working with the StepType Attribute 143 Adding a Header to the Wizard Control 143 Working with the Wizard’s Navigation System 144 Utilizing Wizard Control Events 145 Using the Wizard Control to Show Form Elements 146 imageMap server Control
150 Chart server Control 152 summary 155
chaPTer 4: validaTion server conTrols 157 Understanding Validation 157 Client-side versus server-side Validation 158 asP.neT Validation server Controls 159
Validation Causes 160 The RequiredFieldValidator Server Control 160
The CompareValidator Server Control 164 The RangeValidator Server Control 167 The RegularExpressionValidator Server Control 171 The CustomValidator Server Control 172 The ValidationSummary Server Control 176
Turning off Client-side Validation 179 Using images and sounds for error notifications 180 Working with Validation Groups 181 summary 185
chaPTer 5: working wiTh masTer Pages 187 Why do You need Master Pages? 187 The Basics of Master Pages 189 Coding a Master Page 190 Coding a Content Page
192 Mixing Page Types and Languages 196 Specifying Which Master Page to Use 197 Working with the Page Title
198 Working with Controls and Properties from the Master Page 199 specifying default Content in the Master Page 205 Programmatically assigning the Master Page 207
ConTenTs nesting Master Pages
207 Container-specific Master Pages 211 event ordering 212 Caching with Master Pages 213 asP.neT aJaX and Master Pages 214 summary
216
chaPTer 6: Themes and skins 217 Using asP.neT Themes 217
Applying a Theme to a Single ASP�NET Page 217 Applying a Theme to an Entire Application 219 Removing Themes from Server Controls 219 Removing Themes from Web Pages 220 Understanding Themes When Using Master Pages 221 Understanding the StyleSheetTheme Attribute 221
Creating Your own Themes 222
Creating the Proper Folder Structure 222 Creating a Skin 222 Including CSS Files in Your Themes 224
Having Your Themes Include Images 227 defining Multiple skin options 229 Programmatically Working with Themes 231
Assigning the Page’s Theme Programmatically 231 Assigning a Control’s SkinID Programmatically 231 Themes, skins, and Custom Controls 232 summary
235
chaPTer 7: daTa Binding 237 data source Controls 237
SqlDataSource Control 239 AccessDataSource Control
247 LinqDataSource Control 247 EntityDataSource Control
253 XmlDataSource Control 255 ObjectDataSource Control
256 SiteMapDataSource Control 259 data source Control Caching 259 storing Connection information 260 Using Bound list Controls with data source Controls 262
GridView 262 xix
conTenTs Editing GridView Row Data
275 Deleting GridView Data 281 Other GridView Formatting Features 283
DetailsView 283 Inserting, Updating, and Deleting Data Using DetailsView 287
ListView 289 FormView 296 other databound Controls
300 TreeView
300 AdRotator 301 Menu 301 inline data-Binding syntax
302 Data-Binding Syntax
302
XML Data Binding 303 expressions and expression Builders 303 summary
308
chaPTer 8: daTa managemenT wiTh ado.neT 309 Basic ado.neT features
310 Common ADO�NET Tasks
310 Basic ADO�NET Namespaces and Classes 314 Using the Connection Object
315 Using the Command Object 317 Using the DataReader Object
318 Using DataAdapter 320 Using Parameters 322 Understanding DataSet and DataTable 325
Using Oracle as Your Database with ASP�NET 329 The datalist server Control 330
Looking at the Available Templates 331 Working with ItemTemplate 331 Working with Other Layout Templates 334
Working with Multiple Columns 336 The listView server Control 336
Connecting the ListView to a Database 337 Creating the Layout Template 338 Creating the ItemTemplate
340 Creating the EditItemTemplate 341 Creating the EmptyItemTemplate 341 Creating the InsertItemTemplate 341 Viewing the Results
342 xx
ConTenTs Using Visual studio for ado.neT Tasks 344
Creating a Connection to the Data Source 344 Working with a Dataset Designer 345 Using the CustomerOrders DataSet 348 asynchronous Command execution 352
Asynchronous Methods of the SqlCommand Class 352
IAsyncResult Interface 353 AsyncCallback 354 WaitHandle Class 354 Approaches of Asynchronous Processing in ADO�NET 355
Canceling Asynchronous Processing 370 Asynchronous Connections 370 summary
370
chaPTer 9: querying wiTh linq 371 linQ to objects 371
Understanding Traditional Query Methods 371 Replacing Traditional Queries with LINQ 378 Data Grouping
384 Using Other LINQ Operators 385 Making LINQ Joins 385 Paging Using LINQ 386 linQ to XMl
387 Joining XML Data
390 linQ to sQl
391 Making Insert, Update, and Delete Queries through LINQ 399 Extending LINQ
403 summary
403
chaPTer 10: working wiTh xml and linq To xml 405 The Basics of XMl 406
The XML InfoSet 408
XSD–XML Schema Definition 409 Editing XML and XML Schema in Visual Studio 2010 410 Xmlreader and XmlWriter
413 Using XDocument Rather Than XmlReader 415 Using Schema with XmlTextReader 416 Validating Against a Schema Using an XDocument 417 Including NameTable Optimization 419 Retrieving �NET CLR Types from XML 420 xxi
conTenTs ReadSubtree and XmlSerialization 422 Creating CLR Objects from XML with LINQ to XML 423 Creating XML with XmlWriter
424 Creating XML with LINQ for XML 426 Improvements for XmlReader and XmlWriter 429
Xmldocument and XPathdocument 429 Problems with the DOM
429
XPath, the XPathDocument, and XmlDocument 430 datasets
434 Persisting DataSets to XML
434 XmlDataDocument 435
The Xmldatasource Control 437 XslT 441
XslCompiledTransform 442
XSLT Debugging 445 databases and XMl
446
FOR XML AUTO
446 SQL Server and the XML Data Type 451 summary
456
chaPTer 11: inTroducTion To The Provider model 457 Understanding the Provider 458 The Provider Model in asP.neT 4 459
Setting Up Your Provider to Work with Microsoft SQL Server
7�0, 2000, 2005, or 2008461 Membership Providers 466 Role Providers 469 The Personalization Provider
473 The SiteMap Provider 474 SessionState Providers 476 Web Event Providers 478 Configuration Providers
485 The Web Parts Provider 487
Configuring Providers 489 summary 489
chaPTer 12: exTending The Provider model 491 Providers are one Tier in a larger architecture 491 Modifying Through attribute-Based Programming 492 Simpler Password Structures Through the SqlMembershipProvider 492 Stronger Password Structures Through the SqlMembershipProvider 495 xxii
ConTenTs examining ProviderBase
496 Building Your own Providers 497 Creating the CustomProviders Application 498 Constructing the Class Skeleton Required 499 Creating the XML User Data Store 502 Defining the Provider Instance in the web�config File 502 Not Implementing Methods and Properties of the MembershipProvider Class
503 Implementing Methods and Properties of the MembershipProvider Class 504 Using the XmlMembershipProvider for User Login 510 extending Pre-existing Providers 511
Limiting Role Capabilities with a New LimitedSqlRoleProvider Provider 511 Using the New LimitedSqlRoleProvider Provider 515 summary
518
chaPTer 13: siTe navigaTion 519 XMl-Based sitemaps
520 siteMapPath server Control 521
The PathSeparator Property 523 The PathDirection Property 525 The ParentLevelsDisplayed Property 525
The ShowToolTips Property 526 The SiteMapPath Control’s Child Elements 526
TreeView server Control 527
Identifying the TreeView Control’s Built-In Styles 530 Examining the Parts of the TreeView Control 531 Binding the TreeView Control to an XML File 532 Selecting Multiple Options in a TreeView 534 Specifying Custom Icons in the TreeView Control 537 Specifying Lines Used to Connect Nodes 538 Working with the TreeView Control Programmatically 540
Menu server Control 545
Applying Different Styles to the Menu Control 546 Using Menu Events 550 Binding the Menu Control to an XML File 551 siteMap data Provider
552 ShowStartingNode
552 StartFromCurrentNode 553 StartingNodeOffset 554 StartingNodeUrl
555 siteMap aPi
555 xxiii
conTenTs Url Mapping
557 sitemap localization 558
Structuring the Web�sitemap File for Localization 558 Making Modifications to the Web�config File 559 Creating Assembly Resource (�resx) Files 560 Testing the Results
561 security Trimming
561 Setting Up Role Management for Administrators 562 Setting Up the Administrators’ Section 563 Enabling Security Trimming
564 nesting siteMap files
566 summary 567
chaPTer 14: PersonalizaTion 569 The Personalization Model
570 Creating Personalization Properties 570 Adding a Simple Personalization Property 570 Using Personalization Properties 571 Adding a Group of Personalization Properties 574 Using Grouped Personalization Properties 575 Defining Types for Personalization Properties 576 Using Custom Types
576 Providing Default Values 579 Making Personalization Properties Read-Only 579 anonymous Personalization 579
Enabling Anonymous Identification of the End User 579 Working with Anonymous Identification 582 Anonymous Options for Personalization Properties 583 Warnings about Anonymous User Profile Storage 583
Programmatic access to Personalization 584 Migrating Anonymous Users
584 Personalizing Profiles 585 Determining Whether to Continue with Automatic Saves 586
Personalization Providers 587
Working with SQL Server Express Edition 587 Working with Microsoft’s SQL Server 7�0/2000/2005/2008 588 Using Multiple Providers
590 Managing application Profiles 590
Properties of the ProfileManager Class 591 Methods of the ProfileManager Class 591 Building the ProfileManager�aspx Page 592 xxiv
ConTenTs Examining the ProfileManager�aspx Page’s Code 594 Running the ProfileManager�aspx Page 595 summary
596
chaPTer 15: memBershiP and role managemenT 597 asP.neT 4 authentication 598
Setting Up Your Web Site for Membership 598 Adding Users 600 Asking for Credentials
613 Working with Authenticated Users 620 Showing the Number of Users Online 622 Dealing with Passwords
623 asP.neT 4 authorization
627 Using the LoginView Server Control 627 Setting Up Your Web Site for Role Management 630 Adding and Retrieving Application Roles 632 Deleting Roles
634 Adding Users to Roles 635 Getting All the Users of a Particular Role 635
Getting All the Roles of a Particular User 637 Removing Users from Roles 638 Checking Users in Roles 638 Understanding How Roles Are Cached 639
Using the Web site administration Tool 640 Public Methods of the Membership aPi 640 Public Methods of the roles aPi 641 summary 642
chaPTer 16: PorTal frameworks and weB ParTs 643 introducing Web Parts 643 Building dynamic and Modular Web sites 645
Introducing the WebPartManager Control 645 Working with Zone Layouts 646 Understanding the WebPartZone Control 649
Allowing the User to Change the Mode of the Page 651 Modifying Zones 660
Working with Classes in the Portal framework 666 Creating Custom Web Parts 669 Connecting Web Parts 674
Building the Provider Web Part 675 xxv
conTenTs Building the Consumer Web Part 677 Connecting Web Parts on an ASP�NET Page 679
Understanding the Difficulties in Dealing with Master Pages
When Connecting Web Parts681 summary
682
chaPTer 17: hTml and css design wiTh asP.neT 683 Caveats 684 HTMl and Css overview 684
Creating Style Sheets 685 CSS Rules
687 CSS Inheritance 694 Element Layout and Positioning 695
Working with HTMl and Css in Visual studio 702 Working with CSS in Visual Studio 703 Managing Relative CSS Links in Master Pages 706 Styling ASP�NET Controls
706 summary
708
chaPTer 18: asP.neT ajax 709 Understanding the need for aJaX 709
Before AJAX 710 AJAX Changes the Story 710 asP.neT aJaX and Visual studio 2010 712
Client-Side Technologies 713 Server-Side Technologies 714 Developing with ASP�NET AJAX 714
Building asP.neT aJaX applications 714 Building a Simple ASP�NET Page without AJAX 716 Building a Simple ASP�NET Page with AJAX 718 asP.neT aJaX’s server-side Controls 722
The ScriptManager Control 723 The ScriptManagerProxy Control 725
The Timer Control 726 The UpdatePanel Control
727 The UpdateProgress Control 731
Using Multiple UpdatePanel Controls 733 Working with Page History 737 script Combining
741 summary 745 xxvi
chaPTer 19: asP.neT ajax conTrol ToolkiT 747 downloading and installing the aJaX Control Toolkit 749 The asP.neT aJaX Controls 750 ASP�NET AJAX Control Toolkit Extenders 751 AlwaysVisibleControlExtender 751 AnimationExtender
xxvii ConTenTs
753 AutoCompleteExtender 755 CalendarExtender
757 CollapsiblePanelExtender 758 ColorPickerExtender 760 ConfirmButtonExtender and ModalPopupExtender 761
DragPanelExtender 763 DropDownExtender 764 DropShadowExtender 766 DynamicPopulateExtender 768 FilteredTextBoxExtender 772 HoverMenuExtender 772 ListSearchExtender
774 MaskedEditExtender and MaskedEditValidator 774 MutuallyExclusiveCheckBoxExtender 776 NumericUpDownExtender
778 PagingBulletedListExtender 778 PopupControlExtender
780 ResizableControlExtender 781 RoundedCornersExtender
783 SliderExtender and MultiHandleSliderExtender 784 SlideShowExtender
785 TextBoxWatermarkExtender 788 ToggleButtonExtender 789 UpdatePanelAnimationExtender 790
ValidatorCalloutExtender 791 asP.neT aJaX Control Toolkit server Controls 793
Accordion Control 793 CascadingDropDown 795 NoBot Control 798 PasswordStrength Control 799 Rating Control
800 TabContainer Control 801 summary
803
conTenTs chaPTer 20: securiTy
805 applying authentication Measures 806 The <authentication> Node
806 Windows-Based Authentication 807 Forms-Based Authentication
813 Passport Authentication 821 authenticating specific files and folders 822 Programmatic authorization 822
Working with User�Identity 823 Working with User�IsInRole() 824 Pulling More Information with WindowsIdentity 824 identity and impersonation
827 securing Through iis 828
IP Address and Domain Name Restrictions 829 Working with File Extensions 829 Using the ASP�NET MMC Snap-In 832
Using the IIS 7�0 Manager 832 summary
833
chaPTer 21: sTaTe managemenT 835 Your session state Choices 835 Understanding the session object in asP.neT 838
Sessions and the Event Model 838 Configuring Session State Management 839 In-Process Session State
840 Out-of-Process Session State 846 SQL-Backed Session State
851 Extending Session State with Other Providers 854 Cookieless Session State
855 Choosing the Correct Way to Maintain State 856 The application object
856 Querystrings 857 Cookies 857 PostBacks and Cross-Page PostBacks 857 Hidden fields, Viewstate, and Controlstate 859 Using HttpContext.Current.items for Very short-Term storage 863 summary
864 xxviii
xxix ConTenTs
chaPTer 22: caching 865 Caching 865
Output Caching 865 Partial Page (UserControl) Caching 869
Post-Cache Substitution 870 HttpCachePolicy and Client-Side Caching 871
Caching Programmatically 873
Data Caching Using the Cache Object 873 Controlling the ASP�NET Cache 874 Cache Dependencies
875 �NET 4’s New Object Caching Option 879 Using the sQl server Cache dependency 882
Enabling Databases for SQL Server Cache Invalidation 883 Enabling Tables for SQL Server Cache Invalidation 883 Looking at SQL Server 2000
883 Looking at the Tables That Are Enabled 884 Disabling a Table for SQL Server Cache Invalidation 885 Disabling a Database for SQL Server Cache Invalidation 885 SQL Server 2005 and 2008 Cache Invalidation 885
Configuring Your asP.neT application 886 Testing sQl server Cache invalidation 887 Adding More Than One Table to a Page 890 Attaching SQL Server Cache Dependencies to the Request Object 890 Attaching SQL Server Cache Dependencies to the Cache Object 890 summary
894
chaPTer 23: deBugging and error handling 895 design-Time support 895
Syntax Notifications 896 Immediate and Command Window 897
Task List 898
Tracing 898
System�Diagnostics�Trace and ASP�NET’s Page�Trace 899 Page-Level Tracing 899 Application Tracing 899 Viewing Trace Data 900 Tracing from Components 902 Trace Forwarding 904 TraceListeners 904 Diagnostic Switches 908 Web Events 909
conTenTs debugging