ASP.NET 2.0 : Use Substitution Control for Dynamic sections of a Cached Page

In my earlier articles on ASP.NET 1.x versions I had explained the ways of Caching an ASP.NET Page in which we are limited to two options

i. Caching the whole page using Output Caching - Not applicable in many realtime scenarios where some sections of the page need to be dynamic ex.- Stock Rates

ii. Caching Usercontrols by Fragment Caching - Difficult to implement since page requires to be breaked into separate usercontrols for enabling/disabling caching.

So, if we had situation where we want the whole Page to be cached and only a particular portion of the page not to be cached, we have to separate the page into usercontrols and then provide caching only for those sections for which we need to cache and leave the dynamic section without caching. The reason is that, if you specify outputcaching for the page, then the whole page, including the controls will be cached for that duration.

Whidbey, once again, has come up with a resolution for this, thanks to the Substitution Control to specify a section on an output-cached Web page where you want dynamic content substituted for the control.

The Substitution control offers a simplified solution to partial page caching for pages where the majority of the content is cached. You can output-cache the entire page and then use Substitution controls to specify the parts of the page that are exempt from caching.

The syntax for declaring a substitution control is as follows:-

< asp:substitution id="Substitution1"
methodname="Provide Method Name Here"
runat="Server">
</asp:substitution>


Let us examine how we can check the functionality of the Substitution control on an output cached page.

In our ASPX Page, first we will declare the Output Caching next to the Page Directive as follows:-

<%@ Outputcache duration="60" varybyparam="none" %>

Where the duration="60" specifies that the page will be cached for 60 seconds.

Then, we declare a label, a substitution control and a button control as follows:-

<asp:substitution id="Substitution1"
methodname="GetCurrentDateTime"
runat="Server">
</asp:substitution>

Label displaying the Current Date !!!
<asp:label id="lblCurrentDate"
runat="Server">
</asp:label>

Substitution control displaying the Current Date !!!
<asp:button id="btnRefresh"
text="Check Now !!!"
runat="Server">
</asp:button>




Then, in the codebehind, we specify the text for the Label in the Page_load event as follows:-

void Page_Load(object sender, System.EventArgs e)
{
lblCurrentDate.Text = DateTime.Now.ToString();
}


We will set the Current Date Time to the label so that when we refresh the page, we can see whether the time is retained or new time is displayed on the Label.

Next, we have to define the method that is specified to the Substitution control. If we examine the substitution control declaration above, we can see that we have specified as methodname="GetCurrentDateTime".

When the Substitution control executes, it calls a method that returns a string. The string that the method returns is the content to display on the page at the location of the Substitution control.

We define the method "GetCurrentDateTime" in the codebehind as follows:-

public static string GetCurrentDateTime(HttpContext context)
{
return DateTime.Now.ToString();
}


After compiling the application, if we browse the page we fill find that the text below Label and Substitution control is the same i.e. the Current Date and Time.

However, if you refresh the page by clicking the Button, you will find that the time displayed in the Label remains the same, while the time displayed in the Substitution control is changed (the change might be in seconds).

For subsequent requests also, the time would be changing in substitution control while it remains the same in the label, until the duration of "60" seconds expires.

Once it expires then both the label and substitution control would display the updated current time.

Thus we can see that though we have cached the whole page using output caching, the substitution control is dynamic to show the current time.

This is really a great advantage for developers who would like to use Caching for building robust applications while maintaining dynamic sections of their page as well.

There are other ways to achieve this functionality which I would discuss in my next articles.

Cheers and Happy Programming !!!

Print | posted on Wednesday, May 4, 2005 12:20 PM

Comments on this post

# re: ASP.NET 2.0 : Use Substitution Control for Dynamic sections of a Cached Page

Requesting Gravatar...
Lame, this is just a copy of another web page for using Substitution control. How about some real world examples.
Left by Pedro on Sep 11, 2008 8:25 AM

# re: ASP.NET 2.0 : Use Substitution Control for Dynamic sections of a Cached Page

Requesting Gravatar...
Really nice article ,Short but complete.
Left by Naresh Jakher on Nov 26, 2008 4:34 PM

# re: ASP.NET 2.0 : Use Substitution Control for Dynamic sections of a Cached Page

Requesting Gravatar...
Explanation about the Substitution control is good. How does it work if i am using a datagrid instead of label control?
Left by rupesh krotha on Feb 16, 2009 2:17 PM

# re: ASP.NET 2.0 : Use Substitution Control for Dynamic sections of a Cached Page

Requesting Gravatar...
Interesting,

I learned a lot today thanks to you


Thanks
Left by web development company on Aug 18, 2009 12:17 PM

# re: ASP.NET 2.0 : Use Substitution Control for Dynamic sections of a Cached Page

Requesting Gravatar...
It is very good .
It was very helfull to me.
Thanq you.
Left by bharghavaram on Sep 13, 2009 10:52 PM

# re: ASP.NET 2.0 : Use Substitution Control for Dynamic sections of a Cached Page

Requesting Gravatar...
Explanation about the Substitution control is good. How does it work if i am using a datagrid instead of label control?
Left by mario oyunları on Sep 21, 2009 10:32 AM

# re: ASP.NET 2.0 : Use Substitution Control for Dynamic sections of a Cached Page

Requesting Gravatar...
thanku vry very much for grow up knowledge
Left by harsh on Oct 07, 2009 11:09 PM

# re: ASP.NET 2.0 : Use Substitution Control for Dynamic sections of a Cached Page

Requesting Gravatar...
Thanks for an informative article. However, I must point out that ASP.NET Cache is good for 1-2 server web farms, but, as we get into bigger configurations, it starts giving us problems due to its in-process and standalone nature.
For more information, please visit the link provided.

Cheers.
Left by Jack on ASP.NET Cache on Jan 08, 2010 10:27 PM

# re: ASP.NET 2.0 : Use Substitution Control for Dynamic sections of a Cached Page

Requesting Gravatar...
Thanks for this nice article & example

Left by Rakesh Mohan on Feb 16, 2010 12:09 AM

# re: ASP.NET 2.0 : Use Substitution Control for Dynamic sections of a Cached Page

Requesting Gravatar...
stolen code from http://msdn.microsoft.com/en-us/library/ms228212.aspx
Left by pager on Mar 03, 2010 4:24 PM

# re: ASP.NET 2.0 : Use Substitution Control for Dynamic sections of a Cached Page

Requesting Gravatar...
very good post
Left by ceiling tiles on Apr 11, 2010 10:59 AM

# re: ASP.NET 2.0 : Use Substitution Control for Dynamic sections of a Cached Page

Requesting Gravatar...
These kind of always attractive and I am happy to find so many good point here in the post, writing is simply great, coding is very helpful as well. Thanks for sharing.
Left by Wisdom Teeth Impacted on Apr 19, 2010 7:15 AM

# re: ASP.NET 2.0 : Use Substitution Control for Dynamic sections of a Cached Page

Requesting Gravatar...
good demonstration
Left by sonam on Apr 22, 2010 9:45 PM

# re: ASP.NET 2.0 : Use Substitution Control for Dynamic sections of a Cached Page

Requesting Gravatar...
Excellent thanks for your wonderful insights you posted here.
Left by Commercial Electrician Sydney on May 18, 2010 2:42 PM

# re: ASP.NET 2.0 : Use Substitution Control for Dynamic sections of a Cached Page

Requesting Gravatar...
Submission control seems great, good article...thanks!
Left by Over The Door Mirror on Aug 07, 2010 6:03 AM

# re: ASP.NET 2.0 : Use Substitution Control for Dynamic sections of a Cached Page

Requesting Gravatar...
absolutely useless....copy paste from asp.net website...adding no knowledge at all
Left by Ved on Aug 19, 2010 11:43 AM

# re: ASP.NET 2.0 : Use Substitution Control for Dynamic sections of a Cached Page

Requesting Gravatar...
<asp:substitution id="Substitution1"
methodname="GetCurrentDateTime"
runat="Server">
</asp:substitution>
May be the problem.. Locksmith Melbourne
Left by Locksmith Melbourne on Oct 06, 2010 5:12 PM

# re: ASP.NET 2.0 : Use Substitution Control for Dynamic sections of a Cached Page

Requesting Gravatar...
Thanks a lot for the information. It was very useful!
Left by clickbank products review on Jan 15, 2011 3:52 AM

# re: ASP.NET 2.0 : Use Substitution Control for Dynamic sections of a Cached Page

Requesting Gravatar...
Nice 1...i didn't know anything about this control,but by just reading your article i got the basic idea...
Left by Apurva Mehta on Feb 10, 2011 11:36 AM

Your comment:

 (will show your gravatar)