命名空间:System.Web
程序集:System.Web(在 system.web.dll 中)
![](https://i-blog.csdnimg.cn/blog_migrate/824674f6b2f99367c341175f68d5f007.png)
Public Delegate Function HttpResponseSubstitutionCallback ( _ context As HttpContext _ ) As String
Dim instance As New HttpResponseSubstitutionCallback(AddressOf HandlerMethod)
public delegate string HttpResponseSubstitutionCallback ( HttpContext context )
public delegate String^ HttpResponseSubstitutionCallback (
HttpContext^ context
)
/** @delegate */
public delegate String HttpResponseSubstitutionCallback (
HttpContext context
)
JScript 支持使用委托,但不支持进行新的声明。
参数
-
context
-
包含对页的 HTTP 请求信息的 HttpContext,该页带有需要缓存后替换的控件。
返回值
在发送到客户端之前插入到缓存的响应的内容。![](https://i-blog.csdnimg.cn/blog_migrate/824674f6b2f99367c341175f68d5f007.png)
将 HttpResponseSubstitutionCallback 委托用于 WriteSubstitution 方法,可呈现 Substitution 和 AdRotator 控件缓存后内容。
对于 Substitution 控件,则使用 MethodName 属性来指定要在 Substitution 控件执行时调用的回调方法的名称。您指定的回调方法必须是包含 Substitution 控件的页或用户控件上的静态方法。回调方法的签名必须与接受 HttpResponseSubstitutionCallback 参数并返回字符串的 HttpContext 委托的签名匹配。
对于 AdRotator 控件,除非为 AdCreated 事件提供了事件处理程序,否则该控件的呈现不受页缓存的影响。如果未提供 AdCreated 事件处理程序,则使用缓存后替换来呈现 AdRotator 内容。
![](https://i-blog.csdnimg.cn/blog_migrate/824674f6b2f99367c341175f68d5f007.png)
下面的代码示例演示如何以编程方式将 Substitution 控件添加到输出缓存网页。加载页面时,将在一个标签中向用户显示当前的日期和时间。页面中的此区域每 60 秒缓存并更新一次。当 Substitution 控件执行时,它调用 GetCurrentDateTime 方法,该方法必须匹配 HttpResponseSubstitutionCallback 委托的签名。GetCurrentDateTime 方法返回的字符串将显示给用户。每次刷新页时,都不会缓存和更新页中的这一部分。Substitution 控件的 MethodName 属性获取或设置回调方法的名称。
<%@ outputcache duration="60" varybyparam="none" %> <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> <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>
<%@ outputcache duration="60" varybyparam="none" %> <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> <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>