Share via


@ Implements

Indica que el actual archivo de aplicación ASP.NET (página Web, control de usuario o página maestra) implementa la interfaz de .NET Framework especificada.

<%@ Implements interface="ValidInterfaceName" %>

Atributos

  • interface
    Interfaz que se implementa en la página o el control de usuario.

Comentarios

Cuando se implementa una interfaz en una página de formularios Web Forms, se pueden crear sus eventos, métodos y propiedades entre las etiquetas de apertura y de cierre de un elemento <script> en un bloque de declaraciones de código. Esta directiva no se puede utilizar para implementar una interfaz en un archivo de código en segundo plano.

Ejemplo

En el siguiente ejemplo de código se muestra un control de usuario que incluye una directiva @ Implements para obtener acceso a las seis propiedades de la interfaz IWebPart. Si se implementan estas propiedades en el control de usuario, se le permite tener las propiedades y la apariencia de un control WebPart cuando está colocado dentro de un control WebPartZone. La primera parte del ejemplo de código corresponde al control de usuario; coloque este código en un archivo al que asigne el nombre CalendarUserControl.ascx.

La segunda parte del ejemplo de código corresponde a una página que va a hospedar el control de usuario. Observe que la página utiliza una directiva @ Register para registrar el control de usuario de modo que se pueda usar en la página. Observe asimismo que, cuando se declara el control de usuario en el cuerpo de la página, a algunas propiedades IWebPart, como Title y Description, se les asignan valores en la sintaxis declarativa. Para obtener más información sobre cómo incluir un control de usuario en una página de formularios Web Forms, vea @ Register, Sintaxis de controles de servidor personalizados y el tema Cómo: Incluir un control de usuario en una página web ASP.NET. Para obtener información sobre las páginas de elementos Web, vea Controles de elementos web ASP.NET.

<!-- A user control that implements an interface. -->
<%@ Control language="C#" ClassName="CalendarUserControl" %>
<%@ implements 
    interface="System.Web.UI.WebControls.WebParts.IWebPart" %>

<script runat="server">

  private string m_Description;
  private string m_Title;
  private string m_TitleIconImageUrl;
  private string m_TitleUrl;
  private string m_CatalogIconImageUrl;
  
  [Personalizable]
  public string Description
  {
    get
    {
      object objTitle = ViewState["Description"];
      if (objTitle == null)
        return String.Empty;

      return (string)objTitle;
    }
    set
    {
      ViewState["Description"] = Server.HtmlEncode(value);
    }
  }

  [Personalizable]
  public string Title
  {
    get
    {
      object objTitle = ViewState["Title"];
      if (objTitle == null)
        return String.Empty;

      return (string)objTitle;
    }
    set
    {
      ViewState["Title"] = Server.HtmlEncode(value);
    }
  }

  public string Subtitle
  {
    get
    {
      object objSubTitle = ViewState["Subtitle"];
      if (objSubTitle == null)
        return "Acme Corp";
      return (string)objSubTitle;
    }

  }

  [Personalizable]
  public string TitleIconImageUrl
  {
    get
    {
      object objTitle = ViewState["TitleIconImageUrl"];
      if (objTitle == null)
        return String.Empty;

      return (string)objTitle;
    }
    set
    {
      ViewState["TitleIconImageUrl"] = Server.HtmlEncode(value);
    }
  }

  [Personalizable]
  public string TitleUrl
  {
    get
    {
      object objTitle = ViewState["TitleUrl"];
      if (objTitle == null)
        return String.Empty;

      return (string)objTitle;
    }
    set
    {
      ViewState["TitleUrl"] = Server.HtmlEncode(value);
    }
  }

  [Personalizable]
  public string CatalogIconImageUrl
  {
    get
    {
      object objTitle = ViewState["CatalogIconImageUrl"];
      if (objTitle == null)
        return String.Empty;

      return (string)objTitle;
    }
    set
    {
      ViewState["CatalogIconImageUrl"] = Server.HtmlEncode(value);
    }
  }

</script>
<asp:calendar id="Calendar1" runat="server" />


<!-- A page that registers and hosts the user control. -->
<%@ Page language="C#" %>
<%@ register tagprefix="uc1" 
    tagname="CalControl" 
    src="~/CalendarUserControl.ascx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Calendar Page</title>
</head>
<body>
  <form id="form1" runat="server">
    <asp:webpartmanager id="manager1" runat="server" />
    <asp:webpartzone id="WebPartZone1" runat="server">
      <zonetemplate>
        <uc1:CalControl id="CalControl1" runat="server" 
          title="Personal Calendar" 
          description="My personal calendar for work." />      
      </zonetemplate>
    </asp:webpartzone>
  </form>
</body>
</html>
<!-- A user control that implements an interface. -->
<%@ Control language="VB" ClassName="CalendarUserControl" %>
<%@ implements 
    interface="System.Web.UI.WebControls.WebParts.IWebPart" %>

<script runat="server">

  Private m_Description As String
  Private m_Title As String
  Private m_TitleIconImageUrl As String
  Private m_TitleUrl As String
  Private m_CatalogIconImageUrl As String
  
  <Personalizable()> _
  Public Property Description() As String _
    Implements IWebPart.Description
    Get
      Dim objTitle As Object = ViewState("Description")
      If objTitle Is Nothing Then
        Return String.Empty
      End If
      Return CStr(objTitle)
    End Get
    Set(ByVal value As String)
      ViewState("Description") = Server.HtmlEncode(value)
    End Set
  End Property

  <Personalizable()> _
  Public Property Title() As String _
    Implements IWebPart.Title
    Get
      Dim objTitle As Object = ViewState("Title")
      If objTitle Is Nothing Then
        Return String.Empty
      End If
      Return CStr(objTitle)
    End Get
    Set(ByVal value As String)
      ViewState("Title") = Server.HtmlEncode(value)
    End Set
  End Property

  ReadOnly Property Subtitle() As String _
    Implements IWebPart.Subtitle
    Get
      Dim objSubTitle As Object = ViewState("Subtitle")
      If objSubTitle Is Nothing Then
        Return "Acme Corp"
      End If
      Return CStr(objSubTitle)
    End Get
  End Property

  <Personalizable()> _
  Public Property TitleIconImageUrl() As String _
    Implements IWebPart.TitleIconImageUrl
    Get
      Dim objTitle As Object = ViewState("TitleIconImageUrl")
      If objTitle Is Nothing Then
        Return String.Empty
      End If
      Return CStr(objTitle)
    End Get
    Set(ByVal value As String)
      ViewState("TitleIconImageUrl") = Server.HtmlEncode(value)
    End Set
  End Property

  <Personalizable()> _
  Public Property TitleUrl() As String _
    Implements IWebPart.TitleUrl
    Get
      Dim objTitle As Object = ViewState("TitleUrl")
      If objTitle Is Nothing Then
        Return String.Empty
      End If
      Return CStr(objTitle)
    End Get
    Set(ByVal value As String)
      ViewState("TitleUrl") = Server.HtmlEncode(value)
    End Set
  End Property

  <Personalizable()> _
  Public Property CatalogIconImageUrl() As String _
    Implements IWebPart.CatalogIconImageUrl
    Get
      Dim objTitle As Object = ViewState("CatalogIconImageUrl")
      If objTitle Is Nothing Then
        Return String.Empty
      End If
      Return CStr(objTitle)
    End Get
    Set(ByVal value As String)
      ViewState("CatalogIconImageUrl") = Server.HtmlEncode(value)
    End Set
  End Property

</script>
<asp:calendar id="Calendar1" runat="server" />


<!-- A page that registers and hosts the user control. -->
<%@ Page language="VB" %>
<%@ register tagprefix="uc1" 
    tagname="CalControl" 
    src="~/CalendarUserControl.ascx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Calendar Page</title>
</head>
<body>
  <form id="form1" runat="server">
    <asp:webpartmanager id="manager1" runat="server" />
    <asp:webpartzone id="WebPartZone1" runat="server">
      <zonetemplate>
        <uc1:CalControl id="CalControl1" runat="server" 
          title="Personal Calendar" 
          description="My personal calendar for work." />      
      </zonetemplate>
    </asp:webpartzone>
  </form>
</body>
</html>

Vea también

Tareas

Cómo: Incluir un control de usuario en una página web ASP.NET

Referencia

Sintaxis de directivas de plantilla de texto

Conceptos

Información general sobre sintaxis de páginas web ASP.NET

Modelo de código de las páginas Web ASP.NET

Otros recursos

Controles de elementos web ASP.NET