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.

    Substitution6.gif
     
  • 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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <%@ OutputCache Duration="30" VaryByParam="None" %>
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Substitution Control</title>
    </head>
    <
    body>
        <form id="form1" runat="server">
        <div>
            <asp:Label ID="Label1" Runat="server" Text="Label"></asp:Label>
            <br />
            <asp:Substitution ID="Substitution1" Runat="server"
                 methodName="GetUpdatedTime" />
            <br />
        </div>
        </form>
    </body>
    </
    html
  • 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 " + _
                    context.User.Identity.Name
        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

    Output

    Substitution4.gif
    Substitution5.gif

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.