Using VS - DevSource
DevSource: Microsoft Developer Resource DevSource Home Sponsored by Microsoft Home Add Ons Architecture Languages Techniques Using VS Forums
Home arrow Using VS arrow Page 3 - Dynamically Update Portions of Cached Web Pages with Post-Cache Substitution
Dynamically Update Portions of Cached Web Pages with Post-Cache Substitution
By Julia Lerman

Rate This Article: Add This Article To:

Dynamically Update Portions of Cached Web Pages with Post-Cache Substitution - ' Dynamic Pages '
( Page 3 of 4 )

Cache-Substitution for Dynamic portions of a Cached Page

Let's tackle a simple example. A page on your Web site that imparts the company's history is unlikely to change very often. However, imagine that the Web site design specifies that each page should display the current time. (For simplicity, don't be concerned with the time updating if the user remains on the page for a while. The time stamp should reflect the time when the user first opened the page in the browser.)

ADVERTISEMENT

If we cached the page, the timestamp would never change. Yet, if we want the timestamp to change, then we have to waste server resources to keep spitting out that same page over and over again, just to make the timestamp work.

Post-Cache Substitution

With the Post-Cache Substitution control, we can cache the entire page and have it pulled from the OutputCache, just like any other cached page. However, just before the HTML output is sent back down to the browser, the PostCache Substitution control intercepts the HTML and modifies a portion of the page based on a method that you define in your application. Then the HTML is sent to the browser. In order to do this, the control forces ASP.NET to use server-side caching rather than the browser cache.

The method that populates the substation control must be static (in VB, the keyword is Shared) and it must take the context of the page as a parameter. That enables it to do its magic in the plumbing.

Getting this to work is quite simple.

  1. Create a static method in the code-behind of a Web page that returns a string. This method returns the current time.
  2. [VB]  
      Public Shared Function GetTime(ByVal context As HttpContext) As String
        Return Now.ToLongTimeString
      End Function
    
    [C#]
    public static String GetTime(HttpContext context)
        {
          return Now.ToLongTimeString ();
        }
  3. Drag the Substitution control from the control Toolbox onto the Web form where you would like this information displayed, as you see in Figure 1.
  4. In the control's property window, change MethodName to GetCurrentTime. Now the control knows how to update its content.
  5. To enable OutputCaching on the page, insert the OutputCache directive into the Source of the page, under the Page directive. Here we will only cache for 30 seconds.
  6. <%@ Page Language= ........ %>
    <%@ OutputCache Duration=30 VaryByParam="none" %>
  7. Run the page.
  8. Now you don't really have proof that this is working, so let's insert a timestamp that is cached along with the page, as you see in Figure 2.

  9. Drag a standard ASP.NET Label control onto the page.
  10. Add the following to the Page Load event of the page's code-behind.
  11. Label1.Text = Now.ToLongTimeString
  12. Run the page again, and click on browser's refresh button a number of times.

You will see that the label's timestamp does not change as the page is refreshed because it is being cached; yet, the timestamp displayed by the substitution control is updated each time.

The Subsitution API

Another way to achieve post-cache substation is through code which can be in-line or in your code files.

In the earlier example, instead of dragging the control onto the Web form and modifying its MethodName property, you can embed the following HTML into the source of your page to achieve the same results.

[VB]
<% Response.WriteSubstitution(New HttpResponseSubstitutionCallback(AddressOf _
   GetCurrentTime))%> 
 
[C#]
<% Response.WriteSubstitution(new HttpResponseSubstitutionCallback(GetCurrentTime)); %>

One benefit of Response.WriteSubstitution is that it can also be used in code. This opens up the possibility to incorporate post-cache substitution programmatically into custom server controls or when rendering output in code. The WriteSubstitution method also removes any reliance on a static page method.



 
 
>>> More Using VS Articles          >>> More By Julia Lerman
 



Microsoft's Future: A Chat With Their CTO, Barry Briggs

Play Video >

All Videos >

Julia explores the Robotics Studio!

Read now >

Messages to Bill Gates!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.