May 20, 2019
Hot Topics:

ASP.NET 2.0's New and Improved Caching Features

  • October 27, 2004
  • By Thiru Thangarathinam
  • Send Email »
  • More Articles »

In this attribute declaration, you list the name of the database, followed by the name of the database table. Because of this attribute, any time data in the authors table of the Pubs database changes, the cached data is automatically invalidated. The database name that you are specifying here should already be defined in the connectionStrings section of the web.config file.

As part of the SqlDataSource control declaration, you also specify the SelectCommand, InsertCommand, DeleteCommand, UpdateCommand, and ConnectionString properties. The xxxCommand attributes allow you to specify the SQL command to be executed, and the ConnectionString attribute allows you to specify the connection string to use for connecting to the Pubs database. Again, this value is also retrieved from the web.config file. Because the xxxCommand attributes also contain SQL query parameters, they are specified inside the DeleteParameters, UpdateParameters, and InsertParameters templates.

Finally, declare a GridView control and bind that GridView control to the SqlDataSource control using the DataSourceID attribute. By setting the DataSourceID attribute of the GridView to the ID of the SqlDataSource control, you automatically display the data returned from the SqlDataSource.

Now that you understand the code, test the caching functionality by navigating your browser to the page. You will see an output that is similar to the following:

Click here for a larger image.

The timestamp column displays the "seconds" portion of the date from the database. If you refresh the page again, you will see the same time in the displayed output, because you enabled caching on the SqlDataSource control.

Now, to test the SQL Server-based trigger invalidation, click the Edit hyperlink and update the author's information. You should see a change in the timestamp displayed in the above page. This clearly shows that your SQL Server-based trigger invalidation mechanism automatically invalidates the cached contents of the SqlDataSource control as soon as the data in the authors table changes.

Partial Page Caching

So far, you have seen how to use caching with the SqlDataSource control. This section shows how to use post-cache substitution to replace part of the cached content with refreshed content. This is called partial page caching or page fragment caching. It is a powerful feature that allows an application to use page-level caching, even if part of the page is dynamically generated.

Page fragment caching involves the caching of a fragment of the page, as opposed to the entire page. Sometimes, full-page output caching is not feasible—for example, when portions of the page need to be dynamically created for each user request. In such cases, it can be worthwhile to identify portions of the page or controls that do not often change and that take considerable time and server resources to create. After you identify these portions, you can wrap them in a Web Forms user control and cache the control so that these portions of the page don't need to be recreated each time. This was the only way to implement page fragment caching prior to ASP.NET 2.0. With ASP.NET 2.0's new post cache substitution feature, you can inform the ASP.NET runtime that a particular element, while present on a cached page, should be re-evaluated before the page is presented to the user. You can accomplish this in two ways:

  • Call the new Response.WriteSubstitution method, passing a reference to the substitution callback function.
  • Add an <asp:substitution> control to the Web page and set the MethodName attribute to the name of the callback function.

For either option, you need a @OutputCache directive at the top of the page that specifies the duration and other parameters of the dependency that should be added to the page. For this example, consider the Substitution control.

The Substitution control has one important property named MethodName, which is used to specify the method that will be invoked to provide the dynamic content. The method that the Substitution control calls must be a static method (or Shared method in VB.NET). Furthermore, the method must have one parameter that represents the current HttpContext. To demonstrate this, create a new Web form named PartialPageCaching.aspx and add it to the Web site. The following is the code for the PartialPageCaching.aspx:

<%@ Page Language="C#" %>
<%@ OutputCache Duration="6000" VaryByParam="none" %>
<script runat="server">
    public static string GetRandomNumber(HttpContext context)
        int randomNumber;
        randomNumber = new System.Random().Next(1, 10000);
        return randomNumber.ToString();
  <title>Partial Page Caching using Substitution control</title>
  <form id="form1" runat="server">
   The random number generated is:
   <asp:Substitution ID="Substitution1" MethodName="GetRandomNumber"
   The current time is
   <%= DateTime.Now.ToString("t") %>. It never changes since the
      page is cached.

At the top of the page, the OutputCache directive caches the contents of the page in memory. In the OutputCache directive, you set the Duration attribute to 6,000 milliseconds. The VaryByParam attribute indicates whether or not ASP.NET should take into consideration page parameters when caching. When VaryByParam is set to none, no parameters will be considered; all users will receive the same page no matter what additional parameters are supplied (see the output below).

Click here for a larger image.

The MethodName attribute of the Substitution control is set to a method named GetRandomNumber, which simply returns a random number between 1 and 10,000. When you make a request for the page, the displayed current time always remains the same, whereas the portion of the page that is generated by the substitution control keeps changing every time. In this case, it displays a random number between 1 and 10,000 every time someone requests the page.

Building on the Cache API Foundation

The Cache API in ASP.NET 2.0 builds on the foundation provided by the one in ASP.NET 1.0 and makes building high-performance ASP.NET applications extremely easy and seamless. The new DataSource controls include properties that make it easy to cache database data in memory. By taking advantage of these controls, you can retrieve and cache database data without writing a single line of code. Being able to invalidate a cached item when the data in the database changes is a capability that can go a long way in revolutionizing the way ASP.NET applications are built and deployed. Finally, the new Substitution control enables you to easily inject dynamic content in a cached page.

About the Author

Thiru Thangarathinam has six years of experience in architecting, designing, developing, and implementing applications using object-oriented application development methodologies. He also possesses a thorough understanding of the software life cycle (design, development, and testing). He holds several certifications, including MCAD for .NET, MCSD, and MCP. Thiru is an expert with ASP.NET, .NET Framework, Visual C# .NET, Visual Basic .NET, ADO.NET, XML Web services, and .NET Remoting. Thiru also has authored numerous books and articles. Contact him at thiruthangarathinam@yahoo.com.

Page 3 of 3

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date