共用方式為


@ Implements 

Indicates that the current ASP.NET application file (Web page, user control, or master page) implements the specified .NET Framework interface.

<%@ Implements interface="ValidInterfaceName" %>

Attributes

  • interface
    The interface to be implemented on the page or user control.

Remarks

When you implement an interface in a Web Forms page, you can create its events, methods, and properties between the opening and closing tags of a <script> element in a code declaration block. You cannot use this directive to implement an interface in a code-behind file.

Example

The following code example demonstrates a user control that includes an @ Implements directive to access the six properties of the IWebPart interface. By implementing these properties in the user control, you enable the user control to have the properties and appearance of a WebPart control, when you place it within a WebPartZone control. The first part of the code example is the user control; place this code in a file and name it CalendarUserControl.ascx.

The second part of the code example is a page to host the user control. Note that the page uses an @ Register directive to register the user control for use on the page. Notice also that, when the user control is declared in the body of the page, some IWebPart properties such as Title and Description are assigned values in the declarative syntax. For more information on how to include a user control in a Web Forms page, see @ Register, Custom Server Control Syntax, and How to: Include a User Control in an ASP.NET Web Page. For information about Web Parts pages, see ASP.NET Web Parts Pages.

<!-- 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>

See Also

Tasks

How to: Include a User Control in an ASP.NET Web Page

Reference

Directive Syntax

Concepts

ASP.NET Web Page Syntax Overview
ASP.NET Web Page Code Model

Other Resources

ASP.NET Web Parts Pages