Getting Started
  Introduction
  What is ASP.NET?
  Language Support

ASP.NET Web Forms
  Introducing Web Forms
  Working with Server Controls
  Applying Styles to Controls
  Server Control Form Validation
  Web Forms User Controls
  Data Binding Server Controls
  Server-Side Data Access
  Data Access and Customization
  Working with Business Objects
  Authoring Custom Controls
  Web Forms Controls Reference
  Web Forms Syntax Reference

ASP.NET Web Services
  Introducing Web Services
  Writing a Simple Web Service
  Web Service Type Marshalling
  Using Data in Web Services
  Using Objects and Intrinsics
  The WebService Behavior
  HTML Pattern Matching

ASP.NET Web Applications
  Application Overview
  Using the Global.asax File
  Managing Application State
  HttpHandlers and Factories

Cache Services
  Caching Overview
  Page Output Caching
  Page Fragment Caching
  Page Data Caching

Configuration
  Configuration Overview
  Configuration File Format
  Retrieving Configuration

Deployment
  Deploying Applications
  Using the Process Model
  Handling Errors

Security
  Security Overview
  Authentication & Authorization
  Windows-based Authentication
  Forms-based Authentication
  Authorizing Users and Roles
  User Account Impersonation
  Security and WebServices

Localization
  Internationalization Overview
  Setting Culture and Encoding
  Localizing ASP.NET Applications
  Working with Resource Files

Tracing
  Tracing Overview
  Trace Logging to Page Output
  Application-level Trace Logging

Debugging
  The SDK Debugger

Performance
  Performance Overview
  Performance Tuning Tips
  Measuring Performance

ASP to ASP.NET Migration
  Migration Overview
  Syntax and Semantics
  Language Compatibility
  COM Interoperability
  Transactions

Sample Applications
  A Personalized Portal
  An E-Commerce Storefront
  A Class Browser Application
  IBuySpy.com

  Get URL for this page

Page Fragment Caching

In addition to output caching an entire page, ASP.NET provides a simple way for you to output cache regions of page content, which is appropriately named fragment caching. You delineate regions of your page with a user control, and mark them for caching using the @ OutputCache directive introduced in the previous section. This directive specifies the duration (in seconds) that the output content of the user control should be cached on the server, as well as any optional conditions by which it should be varied.

For example, the following directive instructs ASP.NET to output cache the user control for 120 seconds, and to vary the caching using the "CategoryID" and "SelectedID" querystring or form post parameters.

<%@ OutputCache Duration="120" VaryByParam="CategoryID;SelectedID"%>

The VaryByParam attribute is extremely powerful and allows user control authors to instruct ASP.NET to cache/store multiple instances of an output cache region on the server. For example, the following URLs to the host page of the previous user control cache separate instances of the user control content.

http://localhost/mypage.aspx?categoryid=foo&selectedid=0
http://localhost/mypage.aspx?categoryid=foo&selectedid=1

Logic within a user control can then dynamically generate different content (which is cached separately) depending on the arguments provided.

In addition to supporting the VaryByParam attribute, fragment caching also supports a VaryByControl attribute. Whereas the VaryByParam attribute varies cached results based on name/value pairs sent using POST or GET, the VaryByControl attribute varies the cached fragment by controls within the user control. For example:

<%@ OutputCache Duration="120" VaryByParam="none" VaryByControl="Category" %>

Note that similar to output-cached pages, explict use of VaryByParam is required even if it is not used.

If the user control contained a drop-down select box control named Category, the user control's output would vary based on the selected value within that control.

Just as it is possible to nest user controls recursively within a page (that is, a user control declared within another server control), it is also possible to nest output-cached user controls recursively. This provides a powerful composition model that enables cached regions to be composed of further subcached regions.

The following sample code demonstrates how to cache two menu sections of a page using a declarative user control.


<%@ Register TagPrefix="Acme" TagName="Menu" Src="Menu.ascx" %>

<html>
  <body>
    <table>
      <tr>
        <td>
          <Acme:Menu Category="LeftMenu" runat=server/>
        </td>
        <td>
          <h1>Hi, the time is now: <%=Now%> </h1>
        </td>
        <td>
          <Acme:Menu Category="RightMenu" runat=server/>
        </td>
      <tr>
    </table>
  </body>
</html>
VB

The following sample code shows the implementation of the "Acme:Menu" user control with caching support.


<%@ OutputCache Duration="120" VaryByParam="none" %>

<script language="VB" runat=server>

    Public Category As String;

    Sub Page_Load(sender As Object, e As EventArgs)

        Dim conn As AdoConnection = New AdoConnection("MyDSN")

        MyMenu.DataSource = conn.Execute("select * from menu where category=" & Category)
        MyMenu.DataBind()
    End Sub

</script>

<asp:datagrid id="MyMenu" runat=server/>
VB

Note that this example output caches the response of each user control for a period of 120 seconds. All logic necessary to recreate each menu user control in the event of a cache miss (either because 120 seconds has expired or because memory conditions on the server have become scarce) is encapsulated cleanly within the user control.

The following example shows simple fragment caching. The sample caches the output from a control that retrieves data from an SQL Server database, while keeping the dynamic properties of the parent page. You can see that the page is dynamic because the time is updated with every refresh, while the control is only updated every 60 seconds.

 
VB FragmentCache1.aspx

[Run Sample] | [View Source]

Caveats

Note: Attempts to programmatically manipulate an output-cached control from its containing page result in an error. For example, attempts to use a declarative data binding expression on the user control tag generates parser errors, as shown in the following code.

<!-- The following tags generate parser errors. -->
<Acme:Menu Category='<%# Container.DataItem("Category")'  runat="server"/>

The reason for this is simple. In cases when the content of a user control is output cached, an instance of the control is created only on the first request; thus, once cached, the control is no longer available. Instead, you should encapsulate all the logic necessary to create the content of a user control directly within the control itself; this is typically done within the user control's Page_Load event or Page_PreRender event.

You can declare and use other declarative property parameters to customize the control. For example, the previous user control can be customized as follows:

<Acme:Menu Category="LeftMenu" runat=server/>
<Acme:Menu Category="RightMenu" runat=server/>

These declarations cause the appropriate code to be generated and executed by the page compiler in the event that the control is created as a result of a cache miss. User control developers can then access these settings just as they would in a non-cached user control scenario.

Section Summary

  1. In addition to output caching an entire page, ASP.NET provides a simple way for you to output cache regions of page content, which is appropriately named fragment caching.
  2. You delineate regions of your page with a user control and mark them for caching using the @ OutputCache directive introduced in the previous section.
  3. Just as it is possible to nest user controls recursively within a page (that is, a user control declared within another server control), it is also possible to nest output-cached user controls recursively.
  4. Attempts to programmatically manipulate an output-cached control from its containing page result in an error. Instead, you should encapsulate all the logic necessary to create the content of a user control directly within the control itself, typically within the user control's Page_Load event or Page_PreRender event.
  5. It is possible to declare and use other declarative property parameters to customize the control.


Copyright 2001 Microsoft Corporation. All rights reserved.