You are previewing ASP.NET 2.0 Cookbook, 2nd Edition.
O'Reilly logo
ASP.NET 2.0 Cookbook, 2nd Edition

Book Description

Completely revised for ASP.NET 2.0, this new edition of the best-selling ASP.NET Cookbook has everything you need to go from beginning to advanced Windows-based web site development using Microsoft's popular Visual Studio 2005 and ASP.NET 2.0 developer tools. Written for the impatient professional, ASP.NET 2.0 Cookbook contains more than 125 recipes for solving common and not-so-common problems you are likely to encounter when building ASP.NET-based web applications.

The recipes in this book, which run the gamut from simple coding techniques to more comprehensive development strategies, are presented in the popular Problem-Solution-Discussion format of the O'Reilly Cookbook series. As with the first edition, every solution is coded in both C# and Visual Basic 2005.

Among the additions and revisions to this new edition are:

  • Three new chapters, including 25 new recipes for Master and Content pages, Personalization using Profiles and Themes, Custom Web Parts, and more

  • New code for every solution, rewritten to take advantage of features and techniques new to ASP.NET 2.0 and available for download

The ASP.NET 2.0 Cookbook continues to provide the most comprehensive coverage you'll find anywhere of:

  • Tabular controls, including the new GridView control

  • Data validation, including the new ASP.NET 2.0 validation controls, as well as techniques for performing your own validation programmatically

  • User and custom controls

  • Error handling, performance tuning, and caching

Whether you're new to ASP.NET or an experienced Microsoft developer, with ASP.NET 2.0 Cookbook, deliverance from a long day (or night) at your computer could be just one recipe away.

Table of Contents

  1. ASP.NET 2.0 Cookbook, 2nd Edition
  2. Preface
    1. 1. What This Book Is About
    2. 2. Who This Book Is For
    3. 3. How This Book Is Organized
    4. 4. Topics Not Covered
    5. 5. Sample Source Code
    6. 6. Sample Database, Scripts, and Connection Strings
    7. 7. Conventions Used in This Book
    8. 8. Using Code Examples
    9. 9. Comments and Questions
    10. 10. Safari Enabled
    11. 11. Acknowledgments
  3. 1. Master Pages
    1. Introduction
    2. 1.1. Generating a Quick Master/Content Page Arrangement
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 1.2. Extending a Master Page’s Content to Include Content for Other Application Pages
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 1.3. Changing Which Master Page Is Used Without Modifying All Affected Application Pages
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 1.4. Setting the Master Page at Runtime
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  4. 2. Tabular Data
    1. Introduction
    2. 2.1. Selecting the Right Tabular Control
      1. Problem
      2. Solution
      3. Discussion
    3. 2.2. Generating a Quick-and-Dirty Tabular Display
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 2.3. Enhancing the Output of a Tabular Display
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 2.4. Displaying Data from an XML File
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 2.5. Displaying an Array as a Group of Checkboxes
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. 2.6. Displaying Data from a Hashtable
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. 2.7. Adding Next/Previous Navigation to a DataGrid
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. 2.8. Adding First/Last Navigation to a DataGrid
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    10. 2.9. Adding Direct Page Navigation to a DataGrid
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    11. 2.10. Sorting Data in Ascending/Descending Order Within a DataGrid
      1. Problem
      2. Solution
      3. Discussion
    12. 2.11. Combining Sorting and Paging in a DataGrid
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    13. 2.12. Paging Through a Record-Heavy DataGrid
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    14. 2.13. Editing Data Within a DataGrid
      1. Problem
      2. Solution
      3. Discussion
    15. 2.14. Navigating and Sorting Within a GridView
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    16. 2.15. Updating a GridView Without Refreshing the Whole Page
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    17. 2.16. Editing Data in a GridView
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    18. 2.17. Inserting a Row Within a GridView
      1. Problem
      2. Solution
      3. Discussion
    19. 2.18. Formatting Columnar Data in a GridView
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    20. 2.19. Allowing Selection Anywhere Within a GridView
      1. Problem
      2. Solution
      3. Discussion
    21. 2.20. Adding a Delete Confirmation Pop-Up
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    22. 2.21. Displaying a Pop-Up Details Window
      1. Problem
      2. Solution
      3. Discussion
    23. 2.22. Adding a Totals Row to a GridView
      1. Problem
      2. Solution
      3. Discussion
  5. 3. Validation
    1. Introduction
    2. 3.1. Requiring That Data Be Entered in a Field
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 3.2. Requiring Data to Be in a Range
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 3.3. Requiring That Two Data Input Fields Match
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 3.4. Requiring Data to Match a Predefined Pattern
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 3.5. Requiring That a Drop-Down List Selection Be Made
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. 3.6. Requiring Data to Match a Database Entry
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. 3.7. Using Validation Groups to Support Login and New User Registration Within a Single Form
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. 3.8. Performing Validation Programmatically to Execute Your Own Application-Specific Logic
      1. Problem
      2. Solution
      3. Discussion
  6. 4. Forms
    1. Introduction
    2. 4.1. Setting the Default Button to Submit a Form
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 4.2. Submitting a Form to a Different Page
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 4.3. Simulating Multipage Forms Problem
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 4.4. Setting the Initial Focus to a Specific Control Problem
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 4.5. Setting the Focus to a Control with a Validation Error
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  7. 5. User Controls
    1. Introduction
    2. 5.1. Sharing a Page Header on Multiple Pages
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 5.2. Creating a Customizable Navigation Bar
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 5.3. Reusing Code-Behind Classes
      1. Problem
      2. Solution
      3. Discussion
    5. 5.4. Communicating Between User Controls
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 5.5. Adding User Controls Dynamically
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  8. 6. Custom Controls
    1. Introduction
    2. 6.1. Combining HTML Controls in a Single Custom Control
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 6.2. Creating a Custom Control with Attributes
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 6.3. Creating a Custom Control with State
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 6.4. Using the Control State with Custom Controls
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 6.5. Customizing an ASP.NET TextBox Server Control
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  9. 7. Maintaining State
    1. Introduction
    2. 7.1. Maintaining Information Needed by All Users of an Application
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 7.2. Maintaining Information About a User Throughout a Session
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 7.3. Preserving Information Between Postbacks
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 7.4. Preserving Information Across Multiple Requests for a Page
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  10. 8. Error Handling
    1. Introduction
    2. 8.1. Handling Errors at the Method Level
      1. Problem
      2. Solution
      3. Discussion
        1. Basic syntax of Try…Catch…Finally
        2. Guidelines for implementing
        3. Additional considerations
      4. See Also
    3. 8.2. Handling Errors at the Page Level
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 8.3. Handling Errors at the Application Level
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 8.4. Displaying User-Friendly Error Messages
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  11. 9. Security
    1. Introduction
    2. 9.1. Restricting Access to All Application Pages
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 9.2. Restricting Access to Selected Application Pages
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 9.3. Restricting Access to Application Pages by Role
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 9.4. Using Windows Authentication
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 9.5. Using Membership and Roles
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  12. 10. Profiles and Themes
    1. Introduction
    2. 10.1. Using Profiles
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 10.2. Inheriting a Profile
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 10.3. Using and Migrating Anonymous Profiles
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 10.4. Managing User Profiles
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 10.5. Using Themes
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. 10.6. User-Personalized Themes
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  13. 11. Web Parts
    1. Introduction
    2. 11.1. Using Server Controls and User Controls as Web Parts
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 11.2. Creating a Reusable Web Parts Catalog
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 11.3. Creating a Custom Web Part
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 11.4. Communicating Between Web Parts
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 11.5. Persisting Personalized Web Part Properties
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  14. 12. Configuration
    1. Introduction
      1. Configuration File Hierarchy
      2. Structure and Use of web.config
      3. Modifying web.config
    2. 12.1. Overriding Default HTTP Runtime Parameters in web.config
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 12.2. Adding Custom Application Settings in web.config
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 12.3. Displaying Custom Error Messages
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 12.4. Maintaining Session State Across Multiple Web Servers
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 12.5. Accessing Other web.config Configuration Elements
      1. Problem
      2. Solution
      3. Discussion
    7. 12.6. Adding Your Own Configuration Elements to web.config
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. 12.7. Encrypting web.config Sections
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  15. 13. Tracing and Debugging
    1. Introduction
    2. 13.1. Uncovering Page-Level Problems
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 13.2. Uncovering Application-Wide Problems
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 13.3. Pinpointing the Cause of an Exception
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 13.4. Uncovering Problems Within Web Application Components
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 13.5. Uncovering Problems Within Dual-Use Components
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. 13.6. Writing Trace Data to the Event Log with Controllable Levels
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. 13.7. Sending Trace Data via Email with Controllable Levels
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. 13.8. Using a Breakpoint to Stop Execution of an Application When a Condition Is Met
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  16. 14. Web Services
    1. Introduction
    2. 14.1. Creating a Web Service
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 14.2. Consuming a Web Service
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 14.3. Creating a Web Service That Returns a Custom Object
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 14.4. Setting the URL of a Web Service at Runtime
      1. Problem
      2. Solution
      3. Discussion
  17. 15. Dynamic Images
    1. Introduction
    2. 15.1. Drawing Button Images on the Fly
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 15.2. Creating Bar Charts on the Fly
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 15.3. Displaying Images Stored in a Database
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 15.4. Displaying Thumbnail Images
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  18. 16. Caching
    1. Introduction
    2. 16.1. Caching Pages
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 16.2. Caching Pages Based on Query String Parameter Values
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 16.3. Caching Pages Based on Browser Type and Version
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 16.4. Caching Pages Based on Developer-Defined Custom Strings
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 16.5. Caching Pages Based on Database Dependencies
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. 16.6. Caching User Controls
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. 16.7. Caching Application Data
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. 16.8. Caching Application Data Based on Database Dependencies
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    10. 16.9. Caching Data Sources
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  19. 17. Internationalization
    1. Introduction
    2. 17.1. Localizing Request/Response Encoding
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 17.2. Providing Multiple Language Support
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 17.3. Using Global Resources and Overriding Currency Formatting
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  20. 18. File Operations
    1. Introduction
    2. 18.1. Downloading a File from the Web Server
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 18.2. Uploading a File to the Web Server
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 18.3. Processing an Uploaded File Without Storing It on the Filesystem
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 18.4. Storing the Contents of an Uploaded File in a Database
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  21. 19. Performance
    1. Introduction
    2. 19.1. Reducing Page Size by Selectively Disabling the ViewState
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 19.2. Speeding Up String Concatenation with a StringBuilder
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 19.3. Speeding Up Read-Only Data Access
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 19.4. Speeding Up Data Access to a SQL Server Database Using the SQL Provider
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  22. 20. HTTP Handlers
    1. Introduction
    2. 20.1. Creating a Reusable Image Handler
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 20.2. Creating a File Download Handler
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  23. 21. Assorted Tips
    1. Introduction
    2. 21.1. Accessing HTTP-Specific Information from Within a Class
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 21.2. Executing External Applications
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 21.3. Transforming XML to HTML
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 21.4. Determining the User’s Browser Type
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 21.5. Dynamically Creating Browser-Specific Stylesheets
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. 21.6. Saving and Reusing HTML Output
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. 21.7. Sending Mail
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. 21.8. Dynamic Menus
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    10. 21.9. Adding Breadcrumbs
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  24. About the Authors
  25. Colophon
  26. Copyright