ASP.NET Substitution Control using VB.NET

In this article we demonstrate on What is Substitution control and how you can use this control with Output Cache.
  • 2969

Substitution control is one of the new control introduced with ASP.NET. By using this control you can create pages that participate in output caching but also have regions designated to contain dynamic content that will be updated on every page request. This type of setup is referred to as Post Cache Substitution and is aimed at optimizing development experience for Cached page scenario. This control specifies a section on an output Cached web page that is exempt from Caching. You can use Substitution control to specify a section on an Output-Cached web page where you want dynamic content Substituted for the control. A Developer can place a Substitution server control at the page location where content should be substituted and set its Method Name property to the callback method. unlike Response. WriteSubstitution, which can accept a callback method on an arbitrary object, the Substitution server controls Method Name property must be set to static method on the controls containing page or user control.

How to use Substitution Control

  • Simply open a new ASP.NET web application. 
  • Drag a Label and a Substitution control on design page. The page will look like below.

  • You can also add controls from below given code.

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <%@ OutputCache Duration="30" VaryByParam="None" %>
    <html xmlns="" >
        <title>Substitution Control</title>
        <form id="form1" runat="server">
            <asp:Label ID="Label1" Runat="server" Text="Label"></asp:Label>
            <br />
            <asp:Substitution ID="Substitution1" Runat="server"
                 methodName="GetUpdatedTime" />
            <br />
  • Now add the code in Code behind file of web page.

       Public Shared Function GetUpdatedTime(ByVal context As HttpContext) As String
            Return DateTime.Now.ToLongTimeString() + " by " + _
        End Function
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
                Handles Me.Load
            Label1.Text = DateTime.Now.ToLongTimeString()
        End Sub




More Articles

© 2020 DotNetHeaven. All rights reserved.