You are previewing ASP.NET Cookbook.
O'Reilly logo
ASP.NET Cookbook

Book Description

Developers who want to create dynamic, data-driven web sites running on Microsoft web servers have long relied on Active Server Pages (ASP). ASP.NET is Microsoft's latest evolution of ASP. While ASP.NET has a lot in common with its predecessor, this new technology takes advantage of object-oriented programming to dramatically improve developer productivity and convenience. Using the .NET Framework and Microsoft's new object-oriented languages, ASP.NET brings the same rapid drag-and-drop productivity to web applications that the Visual Basic programming language brought to Windows applications. ASP.NET also introduces web services, which allow developers to expose the functionality of an application via HTTP and XML, so that clients on any platform can access it via the Internet. ASP.NET is not a simple upgrade of ASP. It s a quantum leap ahead. There are many benefits to using ASP.NET, and one major drawback: the time developers must devote to mastering this new Web application technology. The ASP.NET Cookbook provides a wealth of plug-and-play solutions to problems commonly encountered when developing ASP.NET web applications and services in the popular problem-solution-discussion Cookbook format. The coding solutions in ASP.NET Cookbook appeal to a wide range of developers, from the inexperienced to the expert. For every problem addressed in the book, there's a worked-out solution or recipe a short, focused piece of code that web developers can insert directly into their applications. Developers can save hours by using just a single one of over 100 recipes included in this invaluable cookbook. But the ASP.NET Cookbook provides far more than just a wide range of cut-and-paste code solutions. Each recipe is followed by a discussion including tips, tricks, and possible pitfalls--so developers can learn to adapt the problem-solving techniques to a myriad of similar situations. Each recipe provides an immediate solution to a pressing problem, while simultaneously allowing developers who prefer to a hands-on learning style with the experience they need to master ASP.NET. This ultimate ASP.NET code sourcebook will quickly earn the dog-eared corners and coffee rings that mark a web developer's most valued resource.

Table of Contents

  1. ASP.NET Cookbook
    1. Preface
      1. What This Book Is About
      2. Who This Book Is For
      3. How This Book Is Organized
      4. Topics Not Covered
      5. Sample Source Code
      6. Sample Database, Scripts, and Connection Strings
      7. Do I Need Visual Studio .NET?
      8. Web Resources
        1. Web Sites
        2. Newsgroups
      9. Conventions Used in This Book
      10. Using Code Examples
      11. Comments and Questions
      12. Acknowledgments
    2. 1. Tabular Data
      1. Introduction
      2. 1.1. Selecting the Right Tabular Control
        1. Problem
        2. Solution
        3. Discussion
      3. 1.2. Generating a Quick-and-Dirty Tabular Display
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 1.3. Enhancing the Output of a Tabular Display
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 1.4. Displaying Data from an XML File
        1. Problem
        2. Solution
        3. Discussion
      6. 1.5. Displaying an Array as a Group of Checkboxes
        1. Problem
        2. Solution
        3. Discussion
      7. 1.6. Displaying Data from a Hashtable
        1. Problem
        2. Solution
        3. Discussion
      8. 1.7. Adding Next/Previous Navigation to a DataGrid
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 1.8. Adding First/Last Navigation to a DataGrid
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. 1.9. Adding Direct Page Navigation to a DataGrid
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. 1.10. Paging Through a Record-Heavy DataGrid
        1. Problem
        2. Solution
        3. Discussion
      12. 1.11. Sorting Data Within a DataGrid
        1. Problem
        2. Solution
        3. Discussion
      13. 1.12. Sorting Data in Ascending/Descending Order Within a DataGrid
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. 1.13. Combining Sorting and Paging in a DataGrid
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. 1.14. Editing Data Within a DataGrid
        1. Problem
        2. Solution
        3. Discussion
      16. 1.15. Formatting Columnar Data in a DataGrid
        1. Problem
        2. Solution
        3. Discussion
      17. 1.16. Allowing Selection Anywhere Within a DataGrid Row
        1. Problem
        2. Solution
        3. Discussion
      18. 1.17. Adding a Delete Confirmation Pop Up
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      19. 1.18. Displaying a Pop-Up Details Window
        1. Problem
        2. Solution
        3. Discussion
      20. 1.19. Adding a Totals Row to a DataGrid
        1. Problem
        2. Solution
        3. Discussion
    3. 2. Validation
      1. Introduction
      2. 2.1. Requiring that Data be Entered in a Field
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 2.2. Requiring Data to Be In a Range
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 2.3. Requiring that Two Data Input Fields Match
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 2.4. Requiring that Data Matches a Predefined Pattern
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 2.5. Requiring that a Drop-Down List Selection Be Made
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 2.6. Requiring Data to Match a Database Entry
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    4. 3. Forms
      1. Introduction
      2. 3.1. Using the Enter Key to Submit a Form
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 3.2. Using the Enter Key to Submit a Form After Validation
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 3.3. Submitting a Form to a Different Page
        1. Problem
        2. Solution
        3. Discussion
      5. 3.4. Simulating Multipage Forms
        1. Problem
        2. Solution
        3. Discussion
        4. See also
      6. 3.5. Setting the Initial Focus to a Specific Control
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 3.6. Setting the Focus to a Control with a Validation Error
        1. Problem
        2. Solution
        3. Discussion
    5. 4. User Controls
      1. Introduction
      2. 4.1. Sharing a Page Header on Multiple Pages
        1. Problem
        2. Solution
        3. Discussion
      3. 4.2. Creating a Customizable Navigation Bar
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 4.3. Reusing Code-Behind Classes
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 4.4. Communicating Between User Controls
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 4.5. Adding User Controls Dynamically
        1. Problem
        2. Solution
        3. Discussion
    6. 5. Custom Controls
      1. Introduction
      2. 5.1. Combining HTML Controls in a Single Custom Control
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 5.2. Creating a Custom Control with Attributes
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 5.3. Creating a Custom Control with State
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 5.4. Customizing an ASP.NET TextBox Server Control
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    7. 6. Maintaining State
      1. Introduction
      2. 6.1. Maintaining Information Needed by All Users of an Application
        1. Problem
        2. Solution
        3. Discussion
      3. 6.2. Maintaining Information about a User Throughout a Session
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 6.3. Preserving Information Between Postbacks
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 6.4. Preserving Information Across Multiple Requests for a Page
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    8. 7. Error Handling
      1. Introduction
      2. 7.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. 7.2. Handling Errors at the Page Level
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 7.3. Handling Errors at the Application Level
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 7.4. Displaying User-Friendly Error Messages
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    9. 8. Security
      1. Introduction
      2. 8.1. Restricting Access to All Application Pages
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 8.2. Restricting Access to Selected Application Pages
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 8.3. Restricting Access to Application Pages by Role
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 8.4. Using Windows Authentication
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    10. 9. Configuration
      1. Introduction
        1. Configuration File Hierarchy
        2. Structure and Use of web.config
        3. Modifying web.config
      2. 9.1. Overriding Default HTTP Runtime Parameters in web.config
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 9.2. Adding Custom Application Settings in web.config
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 9.3. Displaying Custom Error Messages
        1. Problem
        2. Solution
        3. Discussion
      5. 9.4. Maintaining Session State Across Multiple Web Servers
        1. Problem
        2. Solution
        3. Discussion
      6. 9.5. Accessing Other web.config Configuration Elements
        1. Problem
        2. Solution
        3. Discussion
      7. 9.6. Adding Your Own Configuration Elements to web.config
        1. Problem
        2. Solution
        3. Discussion
    11. 10. Tracing and Debugging
      1. Introduction
      2. 10.1. Uncovering Page-Level Problems
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 10.2. Uncovering Problems Application Wide
        1. Problem
        2. Solution
        3. Discussion
      4. 10.3. Pinpointing the Cause of an Exception
        1. Problem
        2. Solution
        3. Discussion
      5. 10.4. Uncovering Problems Within Web Application Components
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 10.5. Uncovering Problems Within Dual-Use Components
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 10.6. Writing Trace Data to the Event Log with Controllable Levels
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 10.7. Using a Breakpoint to Stop Execution of an Application When a Condition Is Met
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 10.8. Stress Testing a Web Application or Service
        1. Problem
        2. Solution
        3. Discussion
    12. 11. Web Services
      1. Introduction
      2. 11.1. Creating a Web Service
        1. Problem
        2. Solution
        3. Discussion
      3. 11.2. Consuming a Web Service
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 11.3. Creating a Web Service That Returns a Custom Object
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 11.4. Setting the URL of a Web Service at Runtime
        1. Problem
        2. Solution
        3. Discussion
    13. 12. Dynamic Images
      1. Introduction
      2. 12.1. Drawing Button Images on the Fly
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 12.2. Creating Bar Charts on the Fly
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 12.3. Displaying Images Stored in a Database
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 12.4. Displaying Thumbnail Images
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    14. 13. Caching
      1. Introduction
      2. 13.1. Caching Pages
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 13.2. Caching Pages Based on Query String Parameter Values
        1. Problem
        2. Solution
        3. Discussion
      4. 13.3. Caching Pages Based on Browser Type and Version
        1. Problem
        2. Solution
        3. Discussion
        4. See also
      5. 13.4. Caching Pages Based on Developer-Defined Custom Strings
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 13.5. Caching User Controls
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 13.6. Caching Application Data
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    15. 14. Internationalization
      1. Introduction
      2. 14.1. Localizing Request/Response Encoding
        1. Problem
        2. Solution
        3. Discussion
      3. 14.2. Providing Multiple Language Support
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 14.3. Overriding Currency Formatting
        1. Problem
        2. Solution
        3. Discussion
    16. 15. File Operations
      1. Introduction
      2. 15.1. Downloading a File from the Web Server
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 15.2. Uploading a File to the Web Server
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 15.3. Processing an Uploaded File Without Storing It on the Filesystem
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 15.4. Storing the Contents of an Uploaded File in a Database
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    17. 16. Performance
      1. Introduction
      2. 16.1. Reducing Page Size by Selectively Disabling the ViewState
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 16.2. Speeding up String Concatenation with a StringBuilder
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. 16.3. Speeding Up Read-Only Data Access
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 16.4. Speeding Up Data Access to a SQL Server Database Using the SQL Provider
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    18. 17. HTTP Handlers
      1. Introduction
      2. 17.1. Creating a Reusable Image Handler
        1. Problem
        2. Solution
        3. Discussion
      3. 17.2. Creating a File Download Handler
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    19. 18. Assorted Tips
      1. Introduction
      2. 18.1. Accessing HTTP-Specific Information from Within a Class
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. 18.2. Executing External Applications
        1. Problem
        2. Solution
        3. Discussion
      4. 18.3. Transforming XML to HTML
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. 18.4. Determining the User’s Browser Type
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. 18.5. Dynamically Creating Browser-Specific Stylesheets
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. 18.6. Saving and Reusing HTML Output
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. 18.7. Sending Mail
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. 18.8. Creating and Using Page Templates
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    20. Index
    21. Colophon