本文章主要介绍了如何使用Substitution控件在缓存页面插入内容。使用Substitution控件将动态内容插入到缓存页中。Substitution控件不会呈现任何标记。您需要将该控件绑定到页上或父用户控件上的方法中。您要自行创建静态方法,以返回要插入到页中的任何信息。由Substitution控件调用的方法必须符合下面的标准。
1. 此方法被定义为静态方法。
2. 此方法接受HttpContext类型的参数。
3. 此方法返回String类型的值。
小编要提醒的是:Substitution控件无法访问页上的其他控件,即用户无法检查或更改其他控件的值。但是,代码可以通过传递给它的参数来访问当前页上下文。
本实例在页面的Load事件中编写代码,设置页面缓存持续时间为60s,将系统时间缓存输出在页面上。使用Substitution控件调用静态方法GetTime更新缓存时间。
下面小编给出主要代码分享给大家
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title>Substitution控件演示</title>
<script runat=”server”>
Static string getCurrentTime(HttpContext context)
{
Return DateTime.Now.ToString();
}
Void Page_Load(object sender,EventArgs e)
{
Response.Cache.SetExpires(DateTime.Now.AddSeconds(600));
Response.Cache.SetCacheability(HttpCacheability.public);
Response.Cache.SetValidUntilExpires(true);
}
</script>
</head>
<body>
<form id=”form1” runat=”server” >
<div>
<h4>使用Substitution控件在缓存页面插入动态内容</h4>
<p>
Cache time:<%=DateTime.Now.ToString%>
</p>
<asp:Substitution ID =”Substitution1” runat=”server” methodname=”getCurrentTime
”/>
<asp:Button ID=”Button1” runat=”server” Text=”刷新页面” Width=”123px”/>
</div>
</form>
</body>
</html>