cyan yelloW MaGenTa Black panTone 123 c Books for professionals By professionals® The eXperT’s Voice® in .neT www.dbebooks.com - Free Books & magazines Companion eBook Available Pro ASP.NET 3.5 in C# 2008: Pro Includes Silverlight 2, Third Edition Pro Dear Reader, i A As you know, ASP.NET is Microsoft’s premier technology for creating server-side n Matthew MacDonald, web applications. ASP.NET 1.0 was a revolution in the web programming world. author of It was so wildly popular that it was licensed on thousands of commercial web Pro Silverlight 2 in C# 2008 servers while it was still a beta product. CS ASP.NET 3.5 Beginning ASP.NET 3.5 In this book, you’ll learn about ASP.NET 3.5, which is the latest milestone in in C# 2008 web development. ASP.NET 3.5 adds a host of minor refinements and two major Pro WPF: Windows features. The first is LINQ—a revolutionary addition that lets you manipulate # Presentation Foundation data, create XML content, and retrieve records from a database without writing P in .NET 3.0 a line of low-level code. The second is ASP.NET AJAX—a toolkit that allows you to Pro .NET 2.0 Windows Forms and Custom Controls in C# create modern, highly responsive web pages that incorporate dynamic effects . and refresh themselves seamlessly. You’ll learn about both of these innovations ASP.NET: The Complete 2 Reference in this book. You’ll also get a solid introduction to Silverlight 2, Microsoft’s next- N C# 2008 generation browser plug-in that allows you to write cross-platform, client-side in applications that run right in the browser, without giving up the conveniences 0 of the C# language. There’s no better way to prepare for the future of the Web. Welcome aboard! E Matthew MacDonald (Microsoft MVP, MCSD) 0 T Includes Silverlight 2 8 THE APRESS ROADMAP Mario Szpuszta, coauthor of Advanced .NET Remoting, Beginning ASP.NET 3.5 Pro ASP.NET 3.5 3 Second Edition in C# 2008, Second Edition in C# 2008, Third Edition Pro Silverlight 2 Pro C# 2008 and the in C# 2008 Beginning C# 2008, Companion eBook .NET 3.5 Platform, Second Edition Fourth Edition . 5 ThIrD EDITIoN See last page for details on $10 eBook version ThIrd EdITIoN Matthew MacDonald SOURCE CODE ONLINE www.apress.com ISBN 978-1-4302-1567-7 55999 Mario Szpuszta and MacDonald, Szpuszta US $59.99 Shelve in .NET User level: 9 781430 215677 Intermediate–Expert this print for content only—size & color not accurate spine = 2.047" 1,520 page count 45# Restore Cote Download at Boykma.Com Macdonald_1567-7front.fm Page i Wednesday, November 19, 2008 3:21 PM Pro ASP.NET 3.5 in C# 2008 Includes Silverlight 2 Third Edition ■ ■ ■ Matthew MacDonald and Mario Szpuszta Download at Boykma.Com Macdonald_1567-7front.fm Page ii Wednesday, November 19, 2008 3:21 PM Pro ASP.NET 3.5 in C# 2008: Includes Silverlight 2, Third Edition Copyright © 2009 by Matthew MacDonald and Mario Szpuszta 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 owner and the publisher. ISBN-13 (paperback): 978-1-4302-1567-7 ISBN-13 (electronic): 978-1-4302-1568-4 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Ewan Buckingham Technical Reviewer: Andy Olsen Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Douglas Pundick, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Production Director | Project Manager: Grace Wong Copy Editors: Kim Wimpsett, Damon Larson Associate Production Director: Kari Brooks-Copony Production Editor: Laura Esterman Compositor: Susan Glinert Proofreader: Nancy Bell Indexer: Broccoli Information Management Artist: April Milne Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http:// 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 http://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. The source code for this book is available to readers at http://www.apress.com in the Source Code section. Download at Boykma.Com Macdonald_1567-7front.fm Page iii Wednesday, November 19, 2008 3:21 PM Download at Boykma.Com Macdonald_1567-7front.fm Page iv Wednesday, November 19, 2008 3:21 PM Contents at a Glance About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxxiii Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxv PART 1 Core Concepts ■ ■ ■ ■ CHAPTER 1 Introducing ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 ■ CHAPTER 2 Visual Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 ■ CHAPTER 3 Web Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 ■ CHAPTER 4 Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 ■ CHAPTER 5 ASP.NET Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 ■ CHAPTER 6 State Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 PART 2 Data Access ■ ■ ■ ■ CHAPTER 7 ADO.NET Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 ■ CHAPTER 8 Data Components and the DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . 295 ■ CHAPTER 9 Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 ■ CHAPTER 10 Rich Data Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381 ■ CHAPTER 11 Caching and Asynchronous Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . 447 ■ CHAPTER 12 Files and Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493 ■ CHAPTER 13 LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527 ■ CHAPTER 14 XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583 PART 3 Building ASP.NET Websites ■ ■ ■ ■ CHAPTER 15 User Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641 ■ CHAPTER 16 Themes and Master Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661 iv Download at Boykma.Com Macdonald_1567-7front.fm Page v Wednesday, November 19, 2008 3:21 PM ■ CHAPTER 17 Website Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691 ■ CHAPTER 18 Website Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741 PART 4 Security ■ ■ ■ ■ CHAPTER 19 The ASP.NET Security Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 823 ■ CHAPTER 20 Forms Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855 ■ CHAPTER 21 Membership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 881 ■ CHAPTER 22 Windows Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 935 ■ CHAPTER 23 Authorization and Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 971 ■ CHAPTER 24 Profiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1005 ■ CHAPTER 25 Cryptography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1037 ■ CHAPTER 26 Custom Membership Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1067 PART 5 Advanced User Interface ■ ■ ■ ■ CHAPTER 27 Custom Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1105 ■ CHAPTER 28 Design-Time Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1147 ■ CHAPTER 29 Dynamic Graphics and GDI+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1179 ■ CHAPTER 30 Portals with Web Part Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1211 PART 6 Client-Side Programming ■ ■ ■ ■ CHAPTER 31 JavaScript and Ajax Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1269 ■ CHAPTER 32 ASP.NET AJAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1319 ■ CHAPTER 33 Silverlight 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1383 ■ INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1441 v Download at Boykma.Com Macdonald_1567-7front.fm Page vi Wednesday, November 19, 2008 3:21 PM Download at Boykma.Com Macdonald_1567-7front.fm Page vii Wednesday, November 19, 2008 3:21 PM Contents About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxxiii Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxv PART 1 Core Concepts ■ ■ ■ ■ CHAPTER 1 Introducing ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 The Evolution of Web Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 The Early Web Development World . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 .NET Revolution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Seven Important Facts About ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Fact 1: ASP.NET Is Integrated with the .NET Framework. . . . . . . . . . 5 Fact 2: ASP.NET Is Compiled, Not Interpreted. . . . . . . . . . . . . . . . . . . 5 Fact 3: ASP.NET Is Multilanguage. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Fact 4: ASP.NET Is Hosted by the Common Language Runtime. . . . 9 Fact 5: ASP.NET Is Object-Oriented . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Fact 6: ASP.NET Is Multidevice and Multibrowser . . . . . . . . . . . . . . 12 Fact 7: ASP.NET Is Easy to Deploy and Configure. . . . . . . . . . . . . . . 12 ASP.NET: The Story Continues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 ASP.NET 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 ASP.NET 3.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 .NET 3.5 Service Pack 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Silverlight. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 ■ CHAPTER 2 Visual Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 The .NET Development Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 The Compiler. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 The Visual Studio IDE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Websites and Web Projects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Creating a Projectless Website . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Multitargeting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Designing a Web Page. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 vii Download at Boykma.Com Macdonald_1567-7front.fm Page viii Wednesday, November 19, 2008 3:21 PM viii ■ CONTENTS The Visual Studio IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Solution Explorer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Document Window. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Toolbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Error List and Task List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Server Explorer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 The Code Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Adding Assembly References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 IntelliSense and Outlining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 The Code Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 How Code-Behind Files Are Connected to Pages . . . . . . . . . . . . . . . 49 How Control Tags Are Connected to Page Variables . . . . . . . . . . . . 50 How Events Are Connected to Event Handlers . . . . . . . . . . . . . . . . . 51 Web Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Creating a Web Project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Migrating a Website from a Previous Version of Visual Studio . . . . 56 Visual Studio Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Single-Step Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Variable Watches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Advanced Breakpoints. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Visual Studio Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 The Web Development Helper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 ■ CHAPTER 3 Web Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Page Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 HTML Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Dynamic User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 The ASP.NET Event Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Automatic Postbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 View State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 XHTML Compliance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Web Forms Processing Stages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Page Framework Initialization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 User Code Initialization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Validation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Automatic Data Binding. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Cleanup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 A Page Flow Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Download at Boykma.Com
Description: