HttpResponseSubstitutionCallback Delegate


Represents the method that handles post-cache substitution.

Namespace:   System.Web
Assembly:  System.Web (in System.Web.dll)

Public Delegate Function HttpResponseSubstitutionCallback (
	context As HttpContext
) As String


Type: System.Web.HttpContext

The HttpContext that contains the HTTP request information for the page with the control that requires post-cache substitution.

Return Value

Type: System.String

The content inserted into the cached response before being sent to the client.

The HttpResponseSubstitutionCallback delegate, in combination with the WriteSubstitution method, is used for rendering post-cache content for the Substitution and AdRotator controls.

For the Substitution control, use the MethodName property to specify the name of the callback method to invoke when the Substitution control executes. The callback method that you specify must be a static method on the page or on the user control that contains the Substitution control. The signature for the callback method must match the signature for an HttpResponseSubstitutionCallback delegate that takes an HttpContext parameter and returns a string.

For the AdRotator control, rendering is independent of the page caching unless an event handler is provided for the AdCreated event. If no AdCreated event handler is present, post-cache substitution is used to render the AdRotator content.

The following code example demonstrates how to add a Substitution control programmatically to an output-cached Web page. When the page loads, the current date and time are displayed to the user in a label. This section of the page is cached and updated every 60 seconds. When the Substitution control executes, it calls the GetCurrentDateTime method, which must match the signature for the HttpResponseSubstitutionCallback delegate. The string that is returned by the GetCurrentDateTime method is displayed to the user. This section of the page is not cached and is updated each time the page is refreshed. The MethodName property of the Substitution control gets or sets the name of the callback method.

<%@ outputcache duration="60" varybyparam="none" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server" language="VB">  

  Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    ' Programmatically create a Substitution control.
    Dim Substitution1 As New Substitution

    ' Specify the callback method.
    Substitution1.MethodName = "GetCurrentDateTime"

    ' Add the Substitution control to the controls
    ' collection of PlaceHolder1.

    ' Display the current date and time in the label.
    ' Output caching applies to this section of the page.
    CachedDateLabel.Text = DateTime.Now.ToString()
  End Sub

  ' The Substitution control calls this method to retrieve
  ' the current date and time. This section of the page
  ' is exempt from output caching. 
  Shared Function GetCurrentDateTime(ByVal context As HttpContext) As String
    Return DateTime.Now.ToString()
  End Function


<html xmlns="" >
<head id="Head1" runat="server">
  <title>Substitution Constructor Example</title>
  <form id="Form1" runat="server">

    <h3>Substitution Constructor Example</h3>  

    <p>This section of the page is not cached:</p>
    <asp:placeholder id="PlaceHolder1"

    <br />

    <p>This section of the page is cached:</p>

    <asp:label id="CachedDateLabel"

    <br /><br />

    <asp:button id="RefreshButton"
      text="Refresh Page"


.NET Framework
Available since 2.0
Return to top