How to Implement Post-Cache Substitution using Substitution control and Response object

Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 9281 red flag

Post substitution cache wllows us to write dynamica content into web page if the page is served from cache.


Post substitution cache allows us to write dynamic content into web page if the page is served from cache and we needed some part of the page should be refresh each time the page is requested by the user. In this case we can use the Substitution control or use response.WriteSubstitution method to render the certain part each time. In order to achieve this, we need to pass the static method to as a parameter to the HttpResponseSubstitutionCallback callback delegate of Response.WriteSubstitution

 object. The engine will not create any instance of the page if the page is serving through the cache rather it will call this callback static method if the response is serving though cache..

ASPX page content

<%@ Page Language="C#" AutoEventWireup="true"CodeBehind="CacheTestPage.aspx.cs"Inherits="WebApplication70515.CacheTestPage" %>

<%@ OutputCache Duration="10" VaryByParam="None" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">

<head runat="server">




<form id="form1" runat="server">



<br />

<asp:Substitution ID="Substitution1" MethodName="GetDateString" runat="server" />

<br />

<asp:Label ID="Label1" runat="server" Text="Get DateTime through Server cache"></asp:Label>:-

<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>

<br />

<asp:Button ID="btnrefresh" runat="server" Text="Refresh" />




In the above code, we have added Substitution control and set static method name as "GetDateString" so that the will call this static method without creating instance of the page class. These static methods simily returns the date time string

ASPX.cs page's content

public partial class CacheTestPage : System.Web.UI.Page


protected void Page_Load(object sender, EventArgs e)


Label2.Text = DateTime.Now.ToString();

Response.WriteSubstitution(new HttpResponseSubstitutionCallback(GetDateTimeString));


public static string GetDateString(HttpContext context)


return "Date time using Substitution cache control <br>" + DateTime.Now.ToString();


public static string GetDateTimeString(HttpContext context)


return "DateTime through response object's write substitute cache method<br>" + DateTime.Now.ToString();



OutPut of the above code shown below screen shot

If we click second time on refresh button then the datetime on last will not refresh but the first two dates will be refresh using Post-Cache Substitution functionality.


Thanks for reading, do let me know your comment or feedback. Keep reading and sharing your knowledge! 


The above article show how we can use the Post-Cache Substitution functionlity in

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Dhirendra Patil
Member Level:
Member Status: Member
Member Since: 3/23/2010 2:39:20 PM
Country: India

Login to vote for this post.

Comments or Responses

Posted by: Antoniacummins on: 3/21/2018 | Points: 25
We will utilize a server side regulator (replacement control) to execute present Cache replacement and on demonstrate the present time for each new Request while perusing alternate substance of the page from the yield Cache. you can visit if you want help .

Posted by: Ydv2125 on: 8/11/2018 | Points: 25
A great things are write on this article and he has impressed me. you can also impress to read this online tutorial it will be helping to remove past searched item in your bing web browser.

Login to post response

Comment using Facebook(Author doesn't get notification)