Caching part of an ASP.Net Page

Sometimes you need to cache only a portion of an ASP.Net page that is not dynamically changing much.  You can achieve partial caching through several methods:

First, You can simply do that by adding the portion that you want to cache to a user control and configure that user control to be cached by adding the page directive to the control header.

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


Doing that the user control will be cached separately from the  ASP.Net page and it will be rendered only once until the cache duration expires.

An alternative to using user controls and caching you can use the Substitution Control which is similar to a label control but is exempted from the the OutputCache directive. You simple set the  Substitution.MethodName with the method responsible for dynamically generating the content, for example, retrieving values from a database. The specified method must accept an HttpContext object as a parameter and returns a string that is going to be written to the response withing the Substitution Control. For example, suppose you want to add a dynamic portion of a cached page that displays the time.
you will add a Substitution Control to the page where you want display the time and then add the following code:

void Page_Load(object sender, System.EventArgs e)
// Specify the callback method.
timeSubstitution.MethodName = "GetTime";
public static string GetTime(HttpContext context)
return DateTime.Now.ToString();