Compartir a través de


Sintaxis declarativa del control de servidor Web Substitution

Actualización: noviembre 2007

Especifica una sección de una página Web con almacenamiento en caché de resultados que está exenta del almacenamiento en caché. En esta ubicación, se recupera el contenido dinámico, que sustituye al control 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"
/>

Comentarios

Utilice el control Substitution para especificar una sección de una página Web con almacenamiento en caché de resultados donde desee que el control se sustituya por contenido dinámico. El control Substitution ofrece una solución simplificada al almacenamiento parcial de páginas en caché en aquellos casos donde la mayor parte del contenido se almacena en caché. Puede almacenar la página completa en la caché de resultados y, a continuación, utilizar controles Substitution para especificar las partes de la página que están exentas del almacenamiento en caché.

Para obtener más información sobre el control Substitution, vea Información general sobre Substitution (Control de servidor Web).

Ejemplo

En el ejemplo de código siguiente se muestra cómo agregar un control Substitution mediante declaración a una página Web con almacenamiento en caché de resultados. Cuando la página se carga, la fecha y hora actual se muestran al usuario en una etiqueta. Esta sección de la página se almacena en caché y se actualiza cada 60 segundos. Cuando el control Substitution se ejecuta, llama al método GetCurrentDateTime. La cadena devuelta por GetCurrentDateTime se muestra al usuario. Esta sección de la página no se almacena en caché y se actualiza cada vez que se actualiza la página.

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

Vea también

Conceptos

Información general sobre Substitution (Control de servidor Web)

Referencia

Substitution