请单击以进行评分并提供反馈
MSDN
MSDN Library
.NET 开发
先前版本
Substitution 类
全部折叠/全部展开 全部折叠
此页面仅适用于
Microsoft Visual Studio 2005/.NET Framework 2.0

同时提供下列产品的其他版本:
.NET Framework 类库
Substitution 类

注意:此类在 .NET Framework 2.0 版中是新增的。

指定输出缓存网页上免于缓存的部分。在此位置,会检索动态内容,并以动态内容替换 Substitution 控件。

命名空间:System.Web.UI.WebControls
程序集:System.Web(在 system.web.dll 中)

Visual Basic(声明)
Public Class Substitution
    Inherits Control
Visual Basic(用法)
Dim instance As Substitution
C#
public class Substitution : Control
C++
public ref class Substitution : public Control
J#
public class Substitution extends Control
JScript
public class Substitution extends Control

使用 Substitution 控件指定输出缓存网页上要以动态内容替换该控件的部分。Substitution 控件为要缓存大部分内容的页提供了一种缓存局部页的简化解决方案。可以对整页进行输出缓存,然后使用 Substitution 控件指定页中免于缓存的部分。需要缓存的区域只执行一次,然后从缓存读取,直至该缓存项到期或被清除。动态区域则在每次请求页时执行。由于不必对这些部分进行封装以缓存在 Web 用户控件中,因此,此缓存模型简化了主要是静态内容的页的代码。例如,如果页包含静态内容(如新闻报道)和显示广告的 AdRotator 控件,这种情况下,此缓存模型就很有用。新闻报道不会更改,这意味着它们可以缓存。但是,您希望在用户每次请求该页时都显示一条新广告。AdRotator 控件直接支持缓存后替换,无论页是否缓存,都在该页回发时呈现一个新广告。

Note注意

在缓存页包含的用户控件中可以放置 Substitution 控件。但是,在输出缓存用户控件中不能放置 Substitution 控件。

Substitution 控件执行时,会调用一个返回字符串的方法。该方法返回的字符串即为要在页中的 Substitution 控件的位置上显示的内容。使用 MethodName 属性指定要在 Substitution 控件执行时调用的回调方法的名称。指定的回调方法必须是包含 Substitution 控件的页或用户控件的静态方法。回调方法的签名必须与接受 HttpContext 参数并返回字符串的 HttpResponseSubstitutionCallback 委托的签名匹配。

若要操作页的输出缓存,可使用 @ OutputCache 指令、HttpCachePolicy 类或 Cache 属性。有关缓存页的更多信息,请参见 缓存 ASP.NET 页缓存 ASP.NET 页的某些部分

Substitution 控件的另一种使用方法是,使用 HttpResponseSubstitutionCallback 委托实现缓存替换行为。此外,还可以在直接支持缓存替换功能的控件(如 AdRotator 控件)上实现缓存替换行为。有关更多信息,请参见 动态更新缓存页的部分

下面的代码示例演示如何以声明方式将 Substitution 控件添加到输出缓存网页。加载页面时,将在一个标签中向用户显示当前的日期和时间。页面中的此区域仅 60 秒便缓存和更新一次。当 Substitution 控件执行时,将调用 GetCurrentDateTime 方法。GetCurrentDateTime 返回的字符串将显示给用户。每次刷新页时,都不会缓存和更新页中的这一部分。

Visual Basic
<%@ outputcache duration="60" varybyparam="none" %>
<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>
<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>
C#
<%@ outputcache duration="60" varybyparam="none" %>
<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>
<head runat="server">
  <title>Substitution Class Example</title>
</head>
<body>
  <form 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>
System.Object
   System.Web.UI.Control
    System.Web.UI.WebControls.Substitution
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

.NET Framework

受以下版本支持:2.0
社区内容   什么是社区内容?
添加新内容 RSS  批注
Processing
© 2009 Microsoft Corporation 版权所有。 保留所有权利 | 商标 | 隐私权声明
Page view tracker