Substitution控件----动态更新缓存页的部分

Substitution控件----动态更新缓存页的部分

www.Phontol.com缓存某个 ASP.NET 页时,默认情况下会缓存该页的全部输出。it.Phontol.com在第一次请求时,该页将运行并缓存其输出。it.Phontol.com对于后续的请求,将通过缓存来完成,该页上的代码不会运行。it.Phontol.com

在某些情况下,可能要缓存 ASP.NET 页,但需根据每个请求更新页上选定的部分。it.Phontol.com例如,您可能要缓存某页的很大一部分,但需要动态更新该页上的与时间高度相关的信息。it.Phontol.com

www.Phontol.com可以使用 Substitution 控件将动态内容插入到缓存页中。it.Phontol.comSubstitution 控件不会呈现任何标记。it.Phontol.com您需要将该控件绑定到页上或父用户控件上的方法中。it.Phontol.com您要自行创建静态方法,以返回要插入到页中的任何信息。it.Phontol.com由 Substitution 控件调用的方法必须符合下面的标准:

  • www.Phontol.com此方法被定义为静态方法(在 Visual Basic 中为共享方法)。it.Phontol.com

  • www.Phontol.com此方法接受 HttpContext 类型的参数。it.Phontol.com

  • www.Phontol.com此方法返回 String 类型的值。it.Phontol.com

www.Phontol.com注意,Substitution 控件无法访问页上的其他控件,也就是说,您无法检查或更改其他控件的值。it.Phontol.com但是,代码确实可以使用传递给它的参数来访问当前页上下文。it.Phontol.com

www.Phontol.com在页运行时,Substitution 控件会调用该方法,然后用从该方法的返回值替换页上的 Substitution 控件。it.Phontol.com

www.Phontol.com下面的代码示例演示如何使用 Substitution 控件在缓存页上创建动态更新的内容。it.Phontol.com页的 Load 事件中的代码用当前时间来更新 Label 控件。it.Phontol.com因为页的缓存持续时间已设置为 60 秒,所以 Label 控件的文本不会更改,即使在 60 秒的时间内多次请求了该页。it.Phontol.com页上的 Substitution 控件调用静态方法 GetTime,该方法将以字符串的形式返回当前时间。it.Phontol.com每次刷新页时,Substitution 控件表示的值都会更新。

 

ContractedBlock.gif ExpandedBlockStart.gif Code
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
 
static string getCurrentTime(HttpContext context)
    {
        
return DateTime.Now.ToString()+ "  <br/>   by  " +context.User.Identity.Name;//读出计算机名称
    }
void Page_Load(object sender, EventArgs e)
{
        Response.Cache.SetExpires(DateTime.Now.AddSeconds(
600));
        Response.Cache.SetCacheability(HttpCacheability.Public);
        Response.Cache.SetValidUntilExpires(
true);

}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>无标题页</title>
</head>
<body>
    
<form id="form1" runat="server">
<div>
    
<h4>
            在缓存页面插入动态内容
--使用substitution控件演示</h4>
        
<p>
             Cache Time:
<%= DateTime.Now.ToString() %>
        
</p>
        
<p>
            
<b>Real Time:<asp:Substitution ID="Substitution1" runat="server" MethodName="getCurrentTime" />
            
</b>
        
</p>
    
</div> 
    
</form>
</body>
</html>

转载于:https://www.cnblogs.com/jianlinglo/archive/2009/01/15/1376375.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值