Share via


Substitution controle de servidor Web Declarative sintaxe

Especifica uma seção em uma página da Web saída da cache que é isenta de cache. Neste local, o conteúdo dinâmico é recuperado e substituirá o Substitution controle.

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

Comentários

Use o Substitution controle para especificar uma seção em um página da Web saída da cache onde você deseja conteúdo dinâmico substituído para o controle. O controle Substitution oferece uma solução simplificada para página com cache parcial para páginas em que a maioria do conteúdo é armazenado em cache. Você pode colocar em cache de saída a página inteira e em seguida, usar controles Substitution para especificar as partes da página que estão isentas do cache.

Para mais informações, consulte Substitution control, see Visão geral do controle servidor da Web Substitution.

Exemplo

O exemplo de código a seguir demonstra como adicionar um Substitution controle declarativamente para uma página output-cached da Web. Quando a página for carregada, a data e time corrente são exibidos ao usuário em um rótulo. Esta seção da página é armazenada em cache e atualizada a cada 60 segundos. Quando o Substitution controle executa, ele chama o GetCurrentDateTime método. A seqüência de caracteres retornada pelo GetCurrentDateTime será exibida para o usuário. Esta seção da página não está armazenada em cache e é atualizada toda vez que a página for atualizada.

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

Consulte também

Conceitos

Visão geral do controle servidor da Web Substitution

Referência

Substitution