How to use Substitution Control in ASP.NET

The substitution control is used to specify a section on an output-cached Web page where you want to substitute the dynamic content for the control. This control provides 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 the Substitution control to specify the sections of the page that are exempted from caching.

Public Properties of the Substitution class

MethodName : Callback method to call up when the Substitution control executes.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="substitution.aspx.cs" Inherits="substitution" %>
<%@ OutputCache Duration="300" VaryByParam="none" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
<script runat="server">  
    static string dotprogramming(HttpContext context) {

        string name = DateTime.Now.ToString();
        return name;
    <form id="form1" runat="server">
    Page Cached DateTime<br />
&nbsp; <%= DateTime.Now.ToString() %> 
        <br />
        Current Time without cache<br />
&nbsp;<asp:Label ID="Label2" runat="server" Text=""></asp:Label>
        <asp:Substitution ID="Substitution1" runat="server" 
            MethodName="dotprogramming" />

In this example page cached for 300 seconds using OutputCache directive , But you can refresh content using Substitution control.
Code Generate the following Output
How to use Substitution Control in ASP.NET

How to use Substitution Control in ASP.NET

Posted on by Jacob Lefore | No comments


Post a Comment