www.it-ebooks.info www.it-ebooks.info www.it-ebooks.info SECOND EDITION Learning ASP.NET 3.5 Jesse Liberty, Dan Hurwitz, and Brian MacDonald Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo www.it-ebooks.info Learning ASP.NET 3.5, Second Edition by Jesse Liberty, Dan Hurwitz, and Brian MacDonald Copyright © 2008 Jesse Liberty, Dan Hurwitz, and Brian MacDonald All rights reserved Printed in the United States of America Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472 O’Reilly books may be purchased for educational, business, or sales promotional use Online editions are also available for most titles (safari.oreilly.com) For more information, contact our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com Editor: John Osborn Production Editor: Sumita Mukherji Proofreader: Sumita Mukherji Indexer: Angela Howard Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Jessamyn Read Printing History: September 2007: First Edition July 2008: Second Edition Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc Learning ASP.NET 3.5, Second Edition, the image of a monkfish, and related trade dress are trademarks of O’Reilly Media, Inc .NET is a registered trademark of Microsoft Corporation Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in this book, and O’Reilly Media, Inc was aware of a trademark claim, the designations have been printed in caps or initial caps While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein This book uses RepKover™, a durable and flexible lay-flat binding ISBN: 978-0-596-51845-5 [M] www.it-ebooks.info Table of Contents Preface xi Getting Started Hello World Creating a New Web Site Creating HelloWorld Making the HelloWorld Web Site Interactive What You Just Did Summary Brain Builder Quiz Exercise 2 13 13 15 15 15 Building Web Applications 17 Mastering Web Site Fundamentals The Page Controls Code-Behind Files Events and Postbacks Synchronous and Asynchronous Postbacks The Page Load event and synchronous postback Adding asynchronous postbacks Using Controls Organizing the Properties Window Finding properties with IntelliSense Basic Controls Creating Tables 17 17 19 22 22 23 27 29 35 35 35 36 39 iii www.it-ebooks.info Setting Properties Selection Controls Panels List Selection Controls Adding items with the Item editor Adding items in Source view More Selection Controls Using Selections to Display Text Images Links LinkButtons Source Code Summary Brain Builder Quiz Exercises 41 43 45 45 46 47 50 52 56 57 58 58 63 65 65 65 Snappier Web Sites with AJAX 69 Take a Walk on the Client Side ScriptManager and UpdatePanel Controlling Browser History Extending Controls with the Control Toolkit TextBoxWaterMarkExtender PopupControlExtender CollapsiblePanelExtender Source Code Listing Summary Brain Builder Quiz Exercises 69 71 76 83 85 89 95 100 105 106 106 106 Saving and Retrieving Data 111 Getting Data from a Database Binding Data Controls Create a Sample Web Page iv | Table of Contents 112 113 115 www.it-ebooks.info Using a DataSource Control Pay No Attention to That Man Behind the Curtain GridView Control Auto-Generated Code Adding Insert, Update, and Delete Statements Displaying and Updating the Data Take It for a Spin Modifying the Grid Based on Conditions Selecting Data from the GridView Passing Parameters to the SELECT Query LINQ Creating the Object Model Using the Object Model Editing Data in LINQ ASP.NET Dynamic Data Source Code Listings Summary Brain Builder Quiz Exercises 115 121 123 125 128 133 134 135 139 140 146 147 153 157 161 166 172 174 174 174 Validation 179 Validation Controls The RequiredFieldValidator The Summary Control The Compare Validator Checking the Input Type Comparing to Another Control Range Checking Regular Expressions Custom Validation Summary Brain Builder Quiz Exercises 180 182 188 189 193 193 195 196 198 201 202 202 202 Table of Contents | v www.it-ebooks.info Style Sheets, Master Pages, and Navigation 207 Styles and Style Sheets Cascading Styles Inline Styles Pros and cons Document-Level Styles Pros and cons External Style Sheets Master Pages Creating a Master Page Adding Content Pages Using Nested Master Pages Changing the Master Page at Runtime Navigation Buttons and HyperLinks Menus and Bread Crumbs Site Maps Using Sitemaps TreeView Customizing the look and feel of the TreeView Replacing the TreeView with a menu control Accessing site map nodes programmatically Bread Crumbs Summary Brain Builder Quiz Exercises 207 208 208 210 210 211 212 220 221 225 228 232 234 235 240 243 246 246 247 248 249 252 255 257 257 257 State and Life Cycle 261 Page Life Cycle State View State Session State Application State Summary Brain Builder Quiz Exercises vi | Table of Contents 261 267 269 279 285 286 288 288 288 www.it-ebooks.info Errors, Exceptions, and Bugs, Oh My! 291 Creating the Sample Application Tracing Page-Level Tracing Inserting into the Trace Log Debugging The Debug Toolbar Breakpoints Setting a breakpoint Breakpoints window Breakpoint properties Breakpoint icons Stepping Through Code Examining Variables and Objects Debug Windows Immediate window Locals window Watch window Call Stack window Error Handling Unhandled Errors Application-Wide Error Pages Page-Specific Error Pages Summary Brain Builder Quiz Exercises 292 295 296 298 300 302 303 303 303 305 307 309 310 310 311 312 313 313 313 314 316 320 320 323 323 323 Security 326 Forms-Based Security Creating Users with the WAT Managing Users Programmatically Creating User Accounts Creating a Welcome Page Creating a Login Page Roles Restricting Access 326 327 332 333 336 337 340 342 Table of Contents | vii www.it-ebooks.info Testing for Login Status Testing for Role-Based Authentication Membership Summary Brain Builder Quiz Exercises 345 345 349 351 351 351 10 Personalization 353 Profiles Simple Data Types Complex Data Types Anonymous Personalization Adding an Anonymous Profile Migrating Anonymous Data to an Actual User’s Record Themes and Skins Create the Test Site Organize Site Themes and Skins Enable Themes and Skins Specify Themes for Your Page Using Named Skins Summary Brain Builder Quiz Exercises 353 354 359 364 365 370 371 372 374 375 377 381 382 384 384 384 11 Putting It All Together 388 Getting Started Adding Styles Using Master Pages Setting Up Roles and Users Logging In Navigation Products Page Adding AJAX Cart Page Purchase Page Confirm Page Custom Error Pages viii | Table of Contents 388 389 392 396 398 402 403 414 414 419 426 428 www.it-ebooks.info Index Symbols &= (concatenate assignment operator), 278 -= (decrement assignment operator), 278 /= (divide assignment operator), 278 = (equal to operator), 31 > (greater than operator), 31 >= (greater than or equal to operator), 31 += (increment assignment operator), 278 < (less than operator), 31 ), 31 greater than or equal to operator (>=), 31 GridView control, 112, 407, 418 adding to page, 123–125 auto-generated code for, 125–127 binding to database tables, 113–121 commands in, modifying appearance of, 133 580 | Index contents of, modifying conditionally, 135–138 in UpdatePanel, 125 LINQ object model used with, 153–160 paging for, enabling, 124, 157 selecting data from, 139 sorting for, enabling, 124, 157 specifying DataSource for, 124 testing, 134 GroupName property, RadioButton control, 44 GUID (Globally Unique Identifier), 367 H Handles keyword, 30 hardware requirements, 453 HasChildNodes property, SiteMapNode object, 251 hashtables, Visual Basic, 276 Hello World application, controls for, 8–13 creating, 6–8 helper methods, 273 Heuer, Tim (Programming Silverlight), 34 hidden fields, view state saved in, 269 history points, 77 history, browser (see browser history) Holzschlag, Molly (The Zen of CSS Design: Visual Enlightenment for the Web), 39 Horovitz, Alex (Programming NET 3.5), 147 hosting web sites (see publishing web sites) HoverMenuExtender control, 84 HTML & XHTML: The Definitive Guide (Musciano; Kennedy), 19, 208 HTML (HyperText Markup Language), 1, 17 (see also markup files) HTML controls, 19 HTTP location option, HTTP status codes, 296 HttpResponse class, 239 Hungarian notation, 26 Hurwitz, Dan Programming ASP.NET, 20, 112, 348 Programming ASP.NET 3.5, xii HyperLink control, 57, 235–239 hyperlinks, 57 www.it-ebooks.info I K ID property, all controls, 35 IDE (Integrated Development Environment), If statements, 137 If-Then statements, 31 IIS, publishing web site to, 498–499 Image control, 56, 97 ImageButton control, 56 ImageControlID property, CollapsiblePanelExtender control, 99 ImageMap control, 56 images, 56 ImageURL property, Image control, 56, 97 Immediate window, for debugging, 311 impedance mismatch, 146 @import statement, 216 increment assignment (+=) operator, 278 Init event, Page, 265 InitialValue property, RequiredFieldValidator control, 184 inline styles, 208–210 inner HTML, 52 inner join, 412 INSERT statement, generating for DataSource, 128–133 Insert Table dialog box, 39 Insert Table Wizard, 39, 52 InsertCommand control, 133 instances, 21 Integrated Development Environment (IDE), IntelliSense, finding properties with, 35 IP address, 480 IsPostBack property, Page, 23, 265 IsValid property, Page, 186, 265 Item editor, adding list items using, 46–47 Kennedy, Bill (HTML & XHTML: The Definitive Guide), 19, 208 Kline, Kevin (Transact-SQL Programming), 412 Krug, Steve (Don’t Make Me Think), 39, 45 J JavaScript AJAX using, 70 custom validators using, 199 modulus operator (%), 200 script blocks written in, 18 writing AJAX controls in, 71 joins, SQL, 412, 415 JScript, RegularExpressionValidator control using, 197 L Label control, Language Integrated Query (see LINQ) layout file, 153 layout, methods of, 42 less than operator (