共用方式為


IWebActionable 介面

定義

WebPart 控制項或其他伺服器控制項能夠容納動詞命令 (Verb) 集合。

public interface class IWebActionable
public interface IWebActionable
type IWebActionable = interface
Public Interface IWebActionable
衍生

範例

下列程式碼範例示範 IWebActionable 介面的簡單實作。 程式代碼範例的第一個部分示範如何在使用者控件中實作 介面。 使用者控制者會 Verbs 建立兩個自定義動詞,並定義當使用者按兩下任一動詞時所呼叫的方法,以實作屬性。 為了簡單起見,這兩個動詞都會使用相同的方法。 方法會更新名為 VerbCounterClicks的屬性值。 方法的程式 Page_Load 代碼會 Verbs 存取 控件的 屬性,以顯示集合中自定義動詞命令的總計數。 此計數不包含標準網頁元件動詞。

重要

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。

<%@ control language="C#" classname="AccountUserControl" %>
<%@ implements 
    interface="System.Web.UI.WebControls.WebParts.IWebActionable" %>
<%@ Import Namespace="System.ComponentModel" %>

<script runat="server">

  private WebPartVerbCollection m_Verbs;
  
  [Personalizable]
  public string UserName
  {
    get
    {
      if (String.IsNullOrEmpty(Textbox1.Text) || 
        Textbox1.Text.Length < 0)
        return String.Empty;
      else
        return Textbox1.Text;
    }
    
    set
    {
      Textbox1.Text = value;
    }
  }
    
  [Personalizable]
  public string Phone
  {
    get
    {
      if(String.IsNullOrEmpty(Textbox2.Text) || 
        Textbox2.Text.Length < 0)
        return String.Empty;
      else
        return Textbox2.Text;
    }
    
    set
    {
      Textbox2.Text = value;
    }
  }

  // The following code handles the verbs.
  [Personalizable]
  public int VerbCounterClicks
  {
    get
    {
      object objVerbCounter = ViewState["VerbCounterClicks"];
      int VerbCounterClicks = 0;
      if (objVerbCounter != null)
        VerbCounterClicks = (int)objVerbCounter;

      return VerbCounterClicks;
    }
    set
    {
      ViewState["VerbCounterClicks"] = value;
    }
  }

  private void IncrementVerbCounterClicks(object sender, 
    WebPartEventArgs e)
  {
    VerbCounterClicks += 1;
    Label4.Text = "Custom Verbs Click Count: " + 
      this.VerbCounterClicks.ToString();
  }

  void Page_Load(object sender, EventArgs e)
  {
    Label3.Text = "Custom Verb Count:  " +
      WebPartManager.GetCurrentWebPartManager(Page).
      WebParts[0].Verbs.Count.ToString();
  }


  // <snippet3>
  // This property implements the IWebActionable interface.
  WebPartVerbCollection IWebActionable.Verbs
  {
    get
    {
      if (m_Verbs == null)
      {
        ArrayList verbsList = new ArrayList();
        WebPartVerb onlyVerb = new WebPartVerb
          ("customVerb1", new WebPartEventHandler(IncrementVerbCounterClicks));
        onlyVerb.Text = "My Verb";
        onlyVerb.Description = "VerbTooltip";
        onlyVerb.Visible = true;
        onlyVerb.Enabled = true;
        verbsList.Add(onlyVerb);
        WebPartVerb otherVerb = new WebPartVerb
          ("customVerb2", new WebPartEventHandler(IncrementVerbCounterClicks));
        otherVerb.Text = "My other Verb";
        otherVerb.Description = "Other VerbTooltip";
        otherVerb.Visible = true;
        otherVerb.Enabled = true;
        verbsList.Add(otherVerb);
        m_Verbs = new WebPartVerbCollection(verbsList);
        return m_Verbs;
      }
      return m_Verbs;
    }
  }
  // </snippet3>
  
</script>
<div>
<asp:label id="Label1" runat="server" AssociatedControlID="Textbox1">
  Name</asp:label>
<asp:textbox id="Textbox1" runat="server" />
</div>
<div>
<asp:label id="Label2" runat="server" AssociatedControlID="Textbox2">
  Phone</asp:label>
<asp:textbox id="Textbox2" runat="server"></asp:textbox>
</div>
<div>
<asp:button id="Button2" runat="server" text="Save Form Values" />
</div>
<hr />
<br />
<asp:Label ID="Label3" runat="server" Text="" />
<br />
<asp:Label ID="Label4" runat="server" Text="" />
<%@ control language="vb" classname="AccountUserControlVB" %>
<%@ implements 
  interface="System.Web.UI.WebControls.WebParts.IWebActionable" %>
<%@ Import Namespace="System.ComponentModel" %>

<script runat="server">

  Private m_Verbs As WebPartVerbCollection

  <Personalizable()> _
  Public Property UserName() As String
    Get
      If String.IsNullOrEmpty(Textbox1.Text) OrElse _
        Textbox1.Text.Length < 0 Then
        Return String.Empty
      Else
        Return Textbox1.Text
      End If
    End Get
    Set(ByVal value As String)
      Textbox1.Text = value
    End Set
  End Property


  <Personalizable()> _
  Public Property Phone() As String
    Get
      If String.IsNullOrEmpty(Textbox2.Text) OrElse _
        Textbox2.Text.Length < 0 Then
        Return String.Empty
      Else
        Return Textbox2.Text
      End If
    End Get
    Set(ByVal value As String)
      Textbox2.Text = value
    End Set
  End Property

  ' The following code handles the verbs.
  <Personalizable()> _
  Public Property VerbCounterClicks() As Integer
    Get
      Dim objVerbCounter As Object = _
        ViewState("VerbCounterClicks")
      VerbCounterClicks = 0
      If Not (objVerbCounter Is Nothing) Then
        VerbCounterClicks = CType(objVerbCounter, Int32)
      End If
      Return VerbCounterClicks
    End Get
    Set(ByVal value As Integer)
      ViewState("VerbCounterClicks") = value
    End Set
  End Property


  Private Sub IncrementVerbCounterClicks _
    (ByVal sender As Object, ByVal e As WebPartEventArgs)
    VerbCounterClicks += 1
    Label4.Text = "Custom Verbs Click Count: " + _
      Me.VerbCounterClicks.ToString()
  End Sub

  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Label3.Text = "Custom Verb Count:  " + _
      WebPartManager.GetCurrentWebPartManager(Page). _
      WebParts(0).Verbs.Count.ToString()
  End Sub

  ' <snippet3>
  ' This property implements the IWebActionable interface.
  ReadOnly Property Verbs() As WebPartVerbCollection _
    Implements IWebActionable.Verbs
    Get
      If (m_Verbs Is Nothing) Then
        Dim verbsList As New ArrayList()
        Dim onlyVerb As New WebPartVerb _
          ("customVerb1", New WebPartEventHandler(AddressOf IncrementVerbCounterClicks))
        onlyVerb.Text = "My Verb"
        onlyVerb.Description = "VerbTooltip"
        onlyVerb.Visible = True
        onlyVerb.Enabled = True
        verbsList.Add(onlyVerb)
        Dim otherVerb As New WebPartVerb _
          ("customVerb2", New WebPartEventHandler(AddressOf IncrementVerbCounterClicks))
        otherVerb.Text = "My other Verb"
        otherVerb.Description = "Other VerbTooltip"
        otherVerb.Visible = True
        otherVerb.Enabled = True
        verbsList.Add(otherVerb)
        m_Verbs = New WebPartVerbCollection(verbsList)
      End If
      Return m_Verbs
    End Get
  End Property
  ' </snippet3>

</script>
<div>
<asp:label id="Label1" runat="server" AssociatedControlID="Textbox1">
  Name</asp:label>
<asp:textbox id="Textbox1" runat="server" />
</div>
<div>
<asp:label id="Label2" runat="server" AssociatedControlID="Textbox2">
  Phone</asp:label>
<asp:textbox id="Textbox2" runat="server"></asp:textbox>
</div>
<div>
<asp:button id="Button2" runat="server" text="Save Form Values" />
</div>
<hr />
<br />
<asp:Label ID="Label3" runat="server" Text="" />
<br />
<asp:Label ID="Label4" runat="server" Text="" />

程式代碼範例的第二個部分是裝載使用者控制項.aspx頁面。 因為控件是在控件中 WebPartZone 參考,所以在運行時間 ASP.NET 將使用者控件包裝在控件中 GenericWebPart ,並將它 WebPart 視為控件。 在瀏覽器中載入頁面之後,請注意控制件底部的標籤會顯示 屬性所參考 Verbs 集合中的自定義動詞數目。 另請注意,如果您按下 控件中的動詞功能表,然後按兩下其中一個動詞,另一個標籤會顯示已按下自定義動詞命令的總次數。

<%@ page language="c#" %>
<%@ register tagprefix="uc1" 
    tagname="AccountUserControl" 
    src="usercontrolverbcs.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 runat="server">
    <title>
      Personalizable User Control with IWebPart Properties
    </title>
  </head>
  <body>
    <form id="form1" runat="server">
      <asp:webpartmanager id="WebPartManager1" runat="server" />
      <asp:webpartzone 
        id="zone1" 
        runat="server" 
        headertext="Main" 
        CloseVerb-Enabled="false">
        <zonetemplate>
          <uc1:AccountUserControl 
            runat="server" 
            id="accountwebpart" 
            title="Account Form" />
        </zonetemplate>
      </asp:webpartzone> 
    </form>
  </body>
</html>
<%@ page language="vb" %>
<%@ register tagprefix="uc1" 
    tagname="AccountUserControlVB" 
    src="usercontrolverbvb.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 runat="server">
    <title>
      Personalizable User Control with IWebPart Properties
    </title>
  </head>
  <body>
    <form id="form1" runat="server">
      <asp:webpartmanager id="WebPartManager1" runat="server" />
      <asp:webpartzone 
        id="zone1" 
        runat="server" 
        headertext="Main" 
        CloseVerb-Enabled="false">
        <zonetemplate>
          <uc1:AccountUserControlVB 
            runat="server" 
            id="accountwebpart" 
            title="Account Form" />
        </zonetemplate>
      </asp:webpartzone> 
    </form>
  </body>
</html>

備註

控制件中的 WebPart 動詞命令是在使用者介面 (UI) 中執行的動作。 一般而言,動詞命令是由按鈕、鏈接或功能表項等可點選控件在 UI 中表示。 Web 元件控制項集提供標準動詞命令,預設可供WebPart控件和其他伺服器控制項使用, (例如自定義、ASP.NET 和使用者控制項,) 在區域中放置WebPartZoneBase控制項時可像控件一樣WebPart。 標準動詞包括關閉、最小化、還原、刪除、編輯和匯出。

您也可以建立與和 伺服器控制件搭配 WebPart 使用的自訂動詞。 介面 IWebActionable 及其 Verbs 屬性可讓您將自定義動詞命令整合到控件中。 類別 WebPart 會實作 IWebActionable 介面,並實作其單一屬性。 若要將自定義動詞新增至繼承自 類別的 WebPart 控件,您必須覆寫 WebPart.Verbs 屬性、將自定義動詞新增至集合,並傳回集合。 屬性所參考的 WebPart.Verbs 集合只包含自定義動詞;此集合中不包含標準動詞。 控件nullWebPart屬性的預設傳回值為 WebPart.Verbs ,因為根據預設,集合中沒有自定義動詞。

將自定義動詞新增至非 WebPart 控件的伺服器控制項需要一個額外的步驟。 在這些情況下,控件也必須實作 IWebActionable 介面並實作 Verbs 屬性。

將自定義動詞新增至 Verbs 集合之後,Web 元件控件集會自動處理建立和轉譯自定義動詞命令所需的其他步驟。

屬性

Verbs

取得自訂 WebPartVerb 物件集合的參考。

適用於

另請參閱