HttpResponseSubstitutionCallback 委托
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示处理缓存后替换的方法。
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,该页带有需要缓存后替换的控件。
返回值
在发送到客户端之前插入到缓存的响应的内容。
示例
下面的代码示例演示如何以编程方式将控件添加到 Substitution 输出缓存网页。 加载页面时,当前日期和时间在标签中向用户显示。 页面的此部分每 60 秒缓存并更新一次。
Substitution控件执行时,它会调用 GetCurrentDateTime
方法,该方法必须与委托的HttpResponseSubstitutionCallback签名匹配。 方法返回 GetCurrentDateTime
的字符串将向用户显示。 页的此部分不会缓存,每次刷新页面时都会更新。 控件 MethodName 的 Substitution 属性获取或设置回调方法的名称。
<%@ 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>
注解
委托HttpResponseSubstitutionCallback与 WriteSubstitution 方法结合使用,用于呈现 和 AdRotator 控件的Substitution后缓存内容。
Substitution对于 控件,使用 MethodName 属性指定要在控件执行时调用的Substitution回调方法的名称。 指定的回调方法必须是页面上或包含 Substitution 该控件的用户控件上的静态方法。 回调方法的签名必须与接受HttpContext参数并返回字符串的HttpResponseSubstitutionCallback委托的签名匹配。
AdRotator对于 控件,除非为 AdCreated 事件提供事件处理程序,否则呈现与页面缓存无关。 AdCreated如果没有事件处理程序,则使用后缓存替换来呈现AdRotator内容。
扩展方法
GetMethodInfo(Delegate) |
获取指示指定委托表示的方法的对象。 |