Поделиться через


Декларативный синтаксис серверного веб-элемента управления Substitution

Обновлен: Ноябрь 2007

Указывает раздел на кэшируемой веб-странице, который исключен из кэширования. В этом расположении осуществляется извлечение динамического содержимого, которое заменяется элементом управления Substitution.

<asp:Substitution
    EnableTheming="True|False"
    EnableViewState="True|False"
    ID="string"
    MethodName="string"
    OnDataBinding="DataBinding event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnUnload="Unload event handler"
    runat="server"
    SkinID="string"
    Visible="True|False"
/>

Заметки

Элемент управления Substitution используется для указания раздела на кэшируемой веб-странице, где динамическое содержимое нужно заменить элементом управления. Элемент управления Substitution предлагает простое решение для частичного кэширования страниц, большая часть содержимого которых уже кэширована. Можно выполнить кэширование всей страницы, а затем с помощью элемента управления Substitution указать те ее части, которые исключены из кэширования.

Дополнительные сведения об элементе управления Substitution см. в разделе Общие сведения о серверном веб-элементе управления Substitution.

Пример

В следующем примере кода показано декларативное добавление элемента управления Substitution на кэшированную веб-страницу. При загрузке страницы в ее заголовке отображаются текущие дата и время. Этот раздел страницы кэшируется и обновляется каждые 60 секунд. Выполняющийся элемент управления Substitution вызывает метод GetCurrentDateTime. Пользователь видит строку, возвращенную методом GetCurrentDateTime. Этот раздел страницы не кэшируется и обновляется при каждом обновлении страницы.

<%@ outputcache duration="60" varybyparam="none" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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 xmlns="http://www.w3.org/1999/xhtml" >
<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>
<%@ outputcache duration="60" varybyparam="none" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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 xmlns="http://www.w3.org/1999/xhtml" >
<head 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>

См. также

Основные понятия

Общие сведения о серверном веб-элементе управления Substitution

Ссылки

Substitution