HttpResponseSubstitutionCallback 委托

定义

表示处理缓存后替换的方法。Represents the method that handles post-cache substitution.

public delegate System::String ^ HttpResponseSubstitutionCallback(HttpContext ^ context);
public delegate string HttpResponseSubstitutionCallback(HttpContext context);
type HttpResponseSubstitutionCallback = delegate of HttpContext -> string
Public Delegate Function HttpResponseSubstitutionCallback(context As HttpContext) As String 

参数

context
HttpContext

包含对页的 HTTP 请求信息的 HttpContext,该页带有需要缓存后替换的控件。The HttpContext that contains the HTTP request information for the page with the control that requires post-cache substitution.

返回值

String

在发送到客户端之前插入到缓存的响应的内容。The content inserted into the cached response before being sent to the client.

继承
HttpResponseSubstitutionCallback

示例

下面的代码示例演示如何以编程方式Substitution将控件添加到输出缓存的网页中。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. 页面的此部分缓存并每隔60秒更新一次。This section of the page is cached and updated every 60 seconds. 当控件执行时, 它会GetCurrentDateTime调用方法, 该方法必须HttpResponseSubstitutionCallback与委托的签名匹配。 SubstitutionWhen the Substitution control executes, it calls the GetCurrentDateTime method, which must match the signature for the HttpResponseSubstitutionCallback delegate. GetCurrentDateTime方法返回的字符串将向用户显示。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. Substitution控件MethodName的属性获取或设置回调方法的名称。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"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server" language="C#">  
  
  void Page_Load(object sender, System.EventArgs e)
  {
    // Programmatically create a Substitution control.
    Substitution Substitution1 = new Substitution();
    
    // Specify the callback method.
    Substitution1.MethodName = "GetCurrentDateTime";
    
    // Add the Substitution control to the controls
    // collection of PlaceHolder1.
    PlaceHolder1.Controls.Add (Substitution1);        

    // Display the current date and time in the label.
    // Output caching applies to this section of the page.
    CachedDateLabel.Text = DateTime.Now.ToString();    
  }
  
  // The Substitution control calls this method to retrieve
  // the current date and time. This section of the page
  // is exempt from output caching. 
  public static string GetCurrentDateTime (HttpContext context)
  {
    return DateTime.Now.ToString ();
  }
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
  <title>Substitution Class Example</title>
</head>
<body>
  <form id="Form1" runat="server">
  
    <h3>Substitution Constructor Example</h3>  
    
    <p>This section of the page is not cached:</p>
    <asp:placeholder id="PlaceHolder1"
      runat="Server">
    </asp:placeholder>
    
    <br />
    
    <p>This section of the page is cached:</p>
    
    <asp:label id="CachedDateLabel"
      runat="Server">
    </asp:label>
    
    <br /><br />
    
    <asp:button id="RefreshButton"
      text="Refresh Page"
      runat="Server">
    </asp:button>     

  </form>
</body>
</html>
<%@ outputcache duration="60" varybyparam="none" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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.
    PlaceHolder1.Controls.Add(Substitution1)
    
    ' 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
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
  <title>Substitution Constructor Example</title>
</head>
<body>
  <form id="Form1" runat="server">
  
    <h3>Substitution Constructor Example</h3>  
    
    <p>This section of the page is not cached:</p>
    <asp:placeholder id="PlaceHolder1"
      runat="Server">
    </asp:placeholder>
    
    <br />
    
    <p>This section of the page is cached:</p>
    
    <asp:label id="CachedDateLabel"
      runat="Server">
    </asp:label>
    
    <br /><br />
    
    <asp:button id="RefreshButton"
      text="Refresh Page"
      runat="Server">
    </asp:button>     

  </form>
</body>
</html>

注解

委托与方法结合使用, 用于为SubstitutionAdRotator控件呈现缓存后内容。 WriteSubstitution HttpResponseSubstitutionCallbackThe HttpResponseSubstitutionCallback delegate, in combination with the WriteSubstitution method, is used for rendering post-cache content for the Substitution and AdRotator controls.

对于控件, 请MethodName使用属性指定在执行Substitution控件时要调用的回调方法的名称。 SubstitutionFor the Substitution control, use the MethodName property to specify the name of the callback method to invoke when the Substitution control executes. 指定的回调方法必须是页面上或包含Substitution控件的用户控件上的静态方法。The callback method that you specify must be a static method on the page or on the user control that contains the Substitution control. 回调方法的签名必须与HttpResponseSubstitutionCallback HttpContext采用参数并返回字符串的委托的签名匹配。The signature for the callback method must match the signature for an HttpResponseSubstitutionCallback delegate that takes an HttpContext parameter and returns a string.

对于控件, 呈现与页缓存无关, 除非为AdCreated事件提供事件处理程序。 AdRotatorFor the AdRotator control, rendering is independent of the page caching unless an event handler is provided for the AdCreated event. 如果不AdCreated存在事件处理程序, 则使用后缓存替换来AdRotator呈现内容。If no AdCreated event handler is present, post-cache substitution is used to render the AdRotator content.

扩展方法

GetMethodInfo(Delegate)

获取指示指定委托表示的方法的对象。Gets an object that represents the method represented by the specified delegate.

适用于

另请参阅