Summary of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv 1. Introductory Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 2. .NET 2.0 Core Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 3. Data Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 4. Pushing the Boundaries of the GridView . . . . . . . . . . . . . . . . . . . . . . . . . 123 5. Form Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 6. Maintaining State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 7. Membership and Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 8. Component-based Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 9. ASP.NET and Web Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 10. Ajax and JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 11. Working with Email . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 12. Rendering Binary Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 13. Handling Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391 14. Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415 15. Performance and Scaling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435 16. Search Engine Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485 17. Advanced Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553 THE ASP.NET 2.0 ANTHOLOGY 101 ESSENTIAL TIPS, TRICKS & HACKS BY SCOTT ALLEN JEFF ATWOOD WYATT BARNETT JON GALLOWAY PHIL HAACK iv The ASP.NET 2.0 Anthology: 101 Essential Tips, Tricks & Hacks by Scott Allen, Jeff Atwood, Wyatt Barnett, Jon Galloway, and Phil Haack Copyright © 2007 SitePoint Pty. Ltd. Expert Reviewer: Wyatt Barnett Editor: Georgina Laidlaw Managing Editor: Simon Mackie Editor: Hilary Reynolds Technical Editor: Matthew Magain Index Editor: Fred Brown Technical Director: Kevin Yank Cover Design: Alex Walker Printing History: First Edition: August 2007 Notice of Rights All rights reserved. No part of this book may be reproduced, stored in a retrieval system or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Notice of Liability The author and publisher have made every effort to ensure the accuracy of the information herein. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors and SitePoint Pty. Ltd., nor its dealers or distributors will be held liable for any damages to be caused either directly or indirectly by the instructions contained in this book, or by the software or hardware products described herein. Trademark Notice Rather than indicating every occurrence of a trademarked name as such, this book uses the names only in an editorial fashion and to the benefit of the trademark owner with no intention of infringement of the trademark. Published by SitePoint Pty. Ltd. 424 Smith Street Collingwood VIC Australia 3066 Web: www.sitepoint.com Email: [email protected] ISBN 978-0-9802858-1-9 Printed and bound in the United States of America v About the Authors Scott Allen is a consultant and founder of OdeToCode.com. Scott is also an instructor for Pluralsight—a premier Microsoft .NET training provider and home to many of the top author ities on .NET today. In 15 years of software development, Scott has shipped commercial software on everything from 8 bit embedded devices to 64 bit web servers. You can reach Scott through his blog at http://www.OdeToCode.com/blogs/scott/. Jeff Atwood lives near Berkeley, California with his wife, two cats, and far more computers than he cares to mention. His first computer was the Texas Instruments TI-99/4a. He’s been a Microsoft Windows developer since 1992. Most of his programming was in Visual Basic, although he spent significant time with early versions of Delphi, and now he’s quite comfort able with C# or VB.NET. Jeff is particularly interested in best practices and human factors in software development, as represented in his blog, http://www.codinghorror.com/. Wyatt Barnett leads the in-house development team for a major industry trade association in Washington DC. He also writes for SitePoint’s .NET blog, Daily Catch, and worked as the Expert Reviewer for this book. After working hard as a submarine lieutenant, Jon Galloway was amazed to find that people would pay him to goof off with computers all day. He spends most of his time with ASP.NET and SQL Server, but likes to keep involved with a variety of other technologies, including Silverlight, Mono, vector graphics, web technologies, and open source .NET development. Jon co-founded the Monoppix project, has contributed to several open source projects, in cluding SubSonic, and regularly releases open source utilities (late at night, when his wife and three daughters are fast asleep). He’s a senior software engineer at Vertigo Software, and blogs at http://weblogs.asp.net/jgalloway/. Phil Haack has over eight years of experience in software development, consulting, and software management, which he puts to good use as the CTO and co-founder of VelocIT. In his spare time, he leads the Subtext open source blogging engine and contributes to various other open source projects. To keep his sanity, he also plays soccer regularly. About the Technical Editor Before joining the SitePoint team as a technical editor, Matthew Magain worked as a software developer for IBM and also spent several years teaching English in Japan. He is the organizer for Melbourne’s Web Standards Group,1 and enjoys candlelit dinners and long walks on the beach. He also enjoys writing bios that sound like they belong in the personals column. Matthew lives with his wife Kimberley and daughter Sophia. 1 http://webstandardsgroup.org/ vi About the Technical Director As Technical Director for SitePoint, Kevin Yank oversees all of its technical publica tions—books, articles, newsletters, and blogs. He has written over 50 articles for SitePoint, but is best known for his book, Build Your Own Database Driven Website Using PHP & MySQL. Kevin lives in Melbourne, Australia, and enjoys performing improvised comedy theater and flying light aircraft. About SitePoint SitePoint specializes in publishing fun, practical, and easy-to-understand content for web professionals. Visit http://www.sitepoint.com/ to access our books, newsletters, articles, and community forums. Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Who Should Read this Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi What’s Covered in this Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi The Book’s Web Site . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix The SitePoint Forums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx The SitePoint Newsletters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx Your Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx Conventions Used in this Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx Chapter 1 Introductory Topics . . . . . . . . . . . . . . . . . . . . . . 1 Which web project model should I choose? . . . . . . . . . . . . . . . . . . . . . . . . . 1 How do I deploy my web site? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 How do I choose a third-party web host? . . . . . . . . . . . . . . . . . . . . . . . . . 12 How do I use source control? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 How do I go about using open source code? . . . . . . . . . . . . . . . . . . . . . . . 21 Where can I find more information about ASP.NET? . . . . . . . . . . . . . . . . 24 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Chapter 2 .NET 2.0 Core Libraries . . . . . . . . . . . . . . . . 29 How do I use strings? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 How do I display an object as a string? . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 How do I display formatted strings? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 How do I use generics? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 How do I filter items in a generic collection? . . . . . . . . . . . . . . . . . . . . . . 45 How can I get more use out of my custom logic? . . . . . . . . . . . . . . . . . . 48 How do I convert generic lists to specific classes? . . . . . . . . . . . . . . . . . . 51 viii How do I concatenate delimited strings from object properties? . . . . . . 53 How do I batch operations with large collections? . . . . . . . . . . . . . . . . . . 56 How do I choose the right collection? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Chapter 3 Data Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 How can I get started using ADO.NET? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 How do I configure my database connection? . . . . . . . . . . . . . . . . . . . . . 73 How do I read data from my database? . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 How do I sort and filter data? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 How do I fill a DropDownList from a database table? . . . . . . . . . . . . . . . 81 How can I perform data binding without having to write all that repetitive code? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 How do I display the contents of a database table? . . . . . . . . . . . . . . . . . 91 How do I allow the modification of a single record? . . . . . . . . . . . . . . . . 99 How can I data bind without using the SqlDataSource? . . . . . . . . . 106 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Chapter 4 Pushing the Boundaries of the GridView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 How do I add a data-bound drop-down to a GridView? . . . . . . . . . . 124 How do I sort on multiple columns? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 How do I display the sort state? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 How do I implement custom paging? . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 How can I allow users to download tabular data as a Microsoft Excel file? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 ix Chapter 5 Form Validation . . . . . . . . . . . . . . . . . . . . . . . . . 159 How do I validate form input? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 How do I validate multiple forms? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 How do I set up custom validation? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 How do I perform custom client-side validation? . . . . . . . . . . . . . . . . . 174 How do I build my own validator control? . . . . . . . . . . . . . . . . . . . . . . . 177 How do I perform client-side validation with my custom validator control? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 Chapter 6 Maintaining State . . . . . . . . . . . . . . . . . . . . . . . 189 How do I maintain per-request state in a web application? . . . . . . . . . 190 How can I maintain session state in a web farm using a state server? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 How can I maintain session state in a web farm using a database? . . . 196 Where should I store application state? . . . . . . . . . . . . . . . . . . . . . . . . . . 201 What’s the cleanest way to access a page’s view state? . . . . . . . . . . . . 206 How can I make sure my custom control works when view state is turned off? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 Chapter 7 Membership and Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 What’s the easiest way to add membership to my site? . . . . . . . . . . . . 220 How do I allow users to register for my site? . . . . . . . . . . . . . . . . . . . . . 222 How do I manage users on my site? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 How do I require users to log in? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 How do I grant access to users who have forgotten their passwords? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 x How do I display content based on roles? . . . . . . . . . . . . . . . . . . . . . . . . 237 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 Chapter 8 Component-based Development . . . 243 How can I use master pages? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 How can my content page override data on my master page? . . . . . . . 248 How can I have my master page interact with my content page? . . . . 253 How do I use URLs in a master page? . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 How do I modify header elements in a master page? . . . . . . . . . . . . . . 260 How do I use themes effectively in conjunction with CSS? . . . . . . . . . 262 How do I treat user controls as components? . . . . . . . . . . . . . . . . . . . . . 268 How do I embed resources into my components? . . . . . . . . . . . . . . . . . 273 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 Chapter 9 ASP.NET and Web Standards . . . . . . . 279 What are all these span tags doing in my HTML output? . . . . . . . . . . . 281 How do I obtain DataList-style functionality without using a table? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 How do I use ASP.NET’s fancy menus without the fancy HTML? . . . . . . 287 How do I make sense of the CSS maze produced by the CSS Friendly menu? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 Chapter 10 Ajax and JavaScript . . . . . . . . . . . . . . . . . . . 303 How can I write better JavaScript? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 How can libraries make writing robust, cross-platform JavaScript easier? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310 How do I use Microsoft's ASP.NET AJAX? . . . . . . . . . . . . . . . . . . . . . . . . . 314 How do I perform partial page rendering? . . . . . . . . . . . . . . . . . . . . . . . 318
Description: