zoukankan      html  css  js  c++  java
  • .net2.0中新增的Substitution控件

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


  • 相关阅读:
    java.lang.IllegalArgumentException: When allowCredentials is true, allowedOrigins cannot contain the special value "*" since that cannot be set on the "Access-Control-Allow-Origin" response header.
    spring-session-data-redis依赖冲突问题
    centos7启动iptables时报Job for iptables.service failed because the control process exited with error cod
    图片上传后台服务报内存溢出 Out Of Memory Java heap space
    mysql 数据库密码忘记重置 进行远程连接
    打Jar包
    Type interface com.innovationV2.mapper.UserMapper is not known to the MapperRegistry
    关于java基础类型Integer String的clone()
    clion使用clang编译
    token & refresh token 机制总结
  • 原文地址:https://www.cnblogs.com/Lewis/p/1118480.html
Copyright © 2011-2022 走看看