Home > ASP.NET Applications > ASP.NET Optimization > ASP.NET Caching Features > Caching Portions of an ASP.NET Page
ASP.NET Web Applications ASP .NET Caching Features
In addition to the ability to output cache multiple versions of entire pages, ASP.NET includes a simple yet powerful way to cache different portions of a single page.
Called fragment caching, this functionality enables subsequent requests for a given portion of a page to be satisfied from the cache so the code that initially generates that portion of the page does not have to be run upon subsequent requests.
Fragment caching is particularly suitable when you expect to return the same information in the same format in certain portions of the page.
To use fragment caching, you identify the parts associated with the request that use up significant server resources to create, such as database queries, and isolate these from the rest of the page. You can then choose which parts of the page are to be declared for caching, and allow the rest of the page to be generated dynamically for each request.
To accomplish this, you must develop a Web Forms user control ( also known as a pagelet ) for each portion of the page that you intend to cache, and include an @ OutputCache directive in the user control file ( the .ascx file ) in much the same way as output caching .aspx files.
For example, if you include the following directive at the top of a user control file, a version of the user control is stored in the output cache for 60 seconds. Note that only the user control is cached by doing so.
<%@ OutputCache Duration=60 VaryByparam = "none" %>
The following example shows a simple use of fragment caching to cache the output of a user control that queries a database and renders the results in a DataList. To verify that output caching is indeed active only in the user control, note the timestamps when the output of the page and the user control are first generated. Reload the page and note that the timestamp for the user control has not changed, indicating that the user control response is being served from the output cache, while the parent page response is generated dynamically for each request.
Just as it is possible to nest or declare a user control within another, you can also nest user controls that have been placed in the output cache. This means that you can include an output cache directive in a user control that is inside an output cached page, or in a user control that is part of another output cached user control. This provides a powerful composition model that enables cached pages or regions to be composed of further subcached regions.
For more information on the attributes you can set on user control output, see the @ OutputCache directive. For more information on how to develop user controls, see Web Forms user controls.
You can include an ID attribute in a user control tag that you have specified for output caching in order to program against that instance of the user control. However, you must explicitly verify the existence of the user control in the output cache so that the code will work properly.
For example, if you declaratively assign an id to a user control ( id = "myUserControl"
), you can check for its existence with the following code in the containing .aspx file’s code declaration block.
void Page_Load ( Object sender, EventArgs e ) {
if ( myUserControl != null ) {
// do whatever you need to here
}
}
Sub Page_Load ( sender As Object, e As EventArgs )
If myUserControl <> Null Then
' do whatever you need to here
End If
End Sub |
|
C# |
VB |
If you write code to manipulate a user control that contains an @ OutputCache directive, an error will occur. A user control that is output cached is only dynamically generated for the first request; any subsequent requests are satisfied from the output cache until the control expires. Any programmatic manipulation that must occur to create the content of the user control must be included in the control. You can include this logic in one of the page lifecycle events associated with the user control, such as in the Page_Load
event or Page_PreRender
event.
Caching Multiple Versions of User Control Output @ OutputCache Web Forms User Controls