共用方式為


@ Implements

表示目前的 ASP.NET 應用程式檔案 (Web 網頁、使用者控制項或主版頁面 (Master Page)) 會實作指定的 .NET Framework 介面。

<%@ Implements interface="ValidInterfaceName" %>

屬性

  • interface
    要在網頁或使用者控制項上實作的介面。

備註

當您在 Web Form 網頁中實作介面時,您可以在程式碼宣告區塊中 <script> 項目的開頭和結尾標記之間,建立其事件、方法和屬性。 您不能使用這個指示詞在程式碼後置檔案中實作介面。

範例

下列程式碼範例會示範一個使用者控制項,其中包含 @ Implements 指示詞以存取 IWebPart 介面的六個屬性。 藉由在使用者控制項實作這些屬性,當您將使用者控制項放置在 WebPartZone 控制項內時,即可使其具有 WebPart 控制項的屬性和外觀。 程式碼範例的第一個部分就是使用者控制項,請將此程式碼置於檔案中,並將其命名為 CalendarUserControl.ascx。

程式碼範例的第二個部分則是要裝載使用者控制項的網頁。 請注意,此網頁會使用 @ Register 指示詞來註冊使用者控制項,以便在網頁上使用。 並請注意,在網頁的主體中宣告使用者控制項時,對於一些 IWebPart 屬性 (例如 TitleDescription) 都是以宣告式語法來指派其值。 如需如何在 Web Form 網頁中包含使用者控制項的詳細資訊,請參閱 @ Register自訂伺服器控制項語法How to: Include a User Control in an ASP.NET Web Page。 如需 Web 組件頁面的資訊,請參閱 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>

請參閱

工作

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

參考

文字範本指示詞語法

概念

ASP.NET Web Page Syntax Overview

ASP.NET Web Page Code Model

其他資源

ASP.NET Web Parts Pages