Substitution Web 服务器控件声明性语法
更新:2007 年 11 月
指定输出缓存的网页中不进行缓存的部分。在此位置,将检索动态内容,并用它们替代 Substitution 控件。
<asp:Substitution
EnableTheming="True|False"
EnableViewState="True|False"
ID="string"
MethodName="string"
OnDataBinding="DataBinding event handler"
OnDisposed="Disposed event handler"
OnInit="Init event handler"
OnLoad="Load event handler"
OnPreRender="PreRender event handler"
OnUnload="Unload event handler"
runat="server"
SkinID="string"
Visible="True|False"
/>
备注
使用 Substitution 控件可以指定输出缓存的网页中希望用动态内容替代控件的部分。对于多数内容都要进行缓存的页,Substitution 控件提供了进行部分页缓存的简化的解决方案。您可以缓存整个页的输出,然后使用 Substitution 控件来指定页中不进行缓存的部分。
有关 Substitution 控件的更多信息,请参见 Substitution Web 服务器控件概述。
示例
下面的代码示例演示如何以声明方式向输出缓存的网页添加一个 Substitution 控件。加载页时,在标签中向用户显示当前的日期和时间。每 60 秒缓存并更新页的此部分。当 Substitution 控件执行时,它调用 GetCurrentDateTime 方法。GetCurrentDateTime 返回的字符串显示给用户。页的此部分不进行缓存,而是在每次刷新页时进行更新。
<%@ 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)
' 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 Class Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>Substitution Class Example</h3>
<p>This section of the page is not cached:</p>
<asp:substitution id="Substitution1"
methodname="GetCurrentDateTime"
runat="Server">
</asp:substitution>
<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="C#">
void Page_Load(object sender, System.EventArgs e)
{
// 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 runat="server">
<title>Substitution Class Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>Substitution Class Example</h3>
<p>This section of the page is not cached:</p>
<asp:substitution id="Substitution1"
methodname="GetCurrentDateTime"
runat="Server">
</asp:substitution>
<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>