IWebActionable 接口

定义

启用 WebPart 控件或其他服务器控件以包含谓词集合。

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

示例

下面的代码示例演示了如何简单实现 IWebActionable 接口。 代码示例的第一部分演示如何在用户控件中实现 接口。 用户控件通过创建两个自定义谓词并定义当用户单击任一谓词时调用的方法来实现 Verbs 属性。 为简单起见,这两个谓词使用相同的方法。 方法更新名为 VerbCounterClicks的属性的值。 方法的代码 Page_Load 访问 Verbs 控件的 属性,以显示集合中自定义谓词的总计数。 此计数不包括标准 Web 部件谓词。

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

<%@ 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控件。 标准谓词包括 close、最小化、还原、删除、编辑和导出。

还可以创建用于 和 服务器控件的 WebPart 自定义谓词。 接口 IWebActionable 及其 Verbs 属性提供了一种将自定义谓词集成到控件中的方法。 类 WebPart 实现 IWebActionable 接口并实现其单个属性。 若要将自定义谓词添加到继承自 类的 WebPart 控件,必须重写 WebPart.Verbs 属性,将自定义谓词添加到集合,然后返回集合。 属性引用的 WebPart.Verbs 集合仅包含自定义谓词;标准谓词不包括在此集合中。 控件nullWebPart 属性的默认返回值为 WebPart.Verbs ,因为默认情况下集合中没有自定义谓词。

将自定义谓词添加到不是 WebPart 控件的服务器控件需要一个额外的步骤。 在这些情况下,控件还必须实现 IWebActionable 接口并实现 Verbs 属性。

将自定义谓词添加到 Verbs 集合后,Web 部件控件集会自动处理创建和呈现自定义谓词所需的其他步骤。

属性

Verbs

获取对自定义 WebPartVerb 对象的集合的引用。

适用于

另请参阅