首页 - 推荐项目 - 精华推荐

.net2.0中新增的Substitution控件

发布时间: 2007-04-22 01:57    作者: 佚名    来源: asp.net中文俱乐部    浏览:    评论

.net2.0中新增的Substitution 控件用在配置为需要进行缓存的 ASP.NET 网页上。Substitution 控件允许您在页上创建一些区域,这些区域可以用动态方式进行更新,然后集成到缓存页。

Substitution控件----动态更新缓存页的部分
缓存某个 ASP.NET 页时,默认情况下会缓存该页的全部输出。在第一次请求时,该页将运行并缓存其输出。对于后续的请求,将通过缓存来完成,该页上的代码不会运行。

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

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

此方法被定义为静态方法(在 Visual Basic 中为共享方法)。

此方法接受 HttpContext 类型的参数。

此方法返回 String 类型的值。

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

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

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

下面用两种方式来未演示这个特性:

代码一:

<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>


<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> 

代码二:

<%@ OutputCache Duration="600" VaryByParam="none" %>

<script runat="server">
 
static string getCurrentTime(HttpContext context)
    
{
        
return DateTime.Now.ToString();
    }


</script>


<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>



结果如图:

 

Smile Big Smile Surprise Stick out tongue Wink Sad Tongue Tied Indifferent Crying Embarrassed Cool Angry Angel Devil [8-|] [:#] [:-*] [:^)] [<:o)] [|-)] Yes Beer Left Hug Music Star Time Snail Pizza Automobile Umbrella Computer Storm [mo] [8o|] [^o)] [+o(] [*-)] [8-)] Coffee No Drinks [Z] Right Hug Cake Broken Heart Gift Wilted Flower Movie Dog Idea Sleep Email Travel Paradise
呢称:

加粗 斜体 下划线 链接 图片 代码 邮件地址 引用 列表

最多只能输入100个字符

Tags

SQL 数据库 asp.net C# XML 控件 .NET教程 程序 事件 数据 安全 代码 Server 客户端 验证 数据库专栏 接口 文件 Oracle DataSet 函数 DataGrid 问题 .net return C#语言 JavaScript 服务 IIS 对象 语句 windows 继承 时间 web.config 设计 开发 参数 变量 解决 字符 ADO.net 环境 VB.Net语言 web 异常 工具 服务器 计算 实例 OLEDB Application VB Word WebService insert asp net 安装 记录

精华推荐

更多

精品下载

更多