Uredi

Deli z drugimi prek


WebPartVerb Class

Definition

Provides an interactive user interface (UI) element that enables users to perform actions on a Web Parts page.

public ref class WebPartVerb : System::Web::UI::IStateManager
[System.ComponentModel.TypeConverter(typeof(System.Web.UI.WebControls.EmptyStringExpandableObjectConverter))]
public class WebPartVerb : System.Web.UI.IStateManager
[<System.ComponentModel.TypeConverter(typeof(System.Web.UI.WebControls.EmptyStringExpandableObjectConverter))>]
type WebPartVerb = class
    interface IStateManager
Public Class WebPartVerb
Implements IStateManager
Inheritance
WebPartVerb
Attributes
Implements

Examples

The following code example shows how to create a custom WebPartVerb object, and a custom zone that uses the OnCreateVerbs method to add the verb to the verbs menu of each WebPart control contained in the zone. There are four parts to the code example:

  • A source file that contains a simple custom WebPart control that displays some text.

  • A source file that contains a custom WebPartVerb object, and a simple custom WebPartZoneBase zone that overrides the OnCreateVerbs method to add the custom verb to WebPart controls in the zone.

  • A Web page that contains the custom zone and the custom WebPart control.

  • An explanation of how the example works.

The first part of the code example contains source code that creates a simple WebPart control that displays some text. For the code example to run, you must compile this source code. You can compile it explicitly and put the resulting assembly in your Web site's Bin folder or the global assembly cache. Alternatively, you can put the source code in your site's App_Code folder, where it will be dynamically compiled at run time. This code example uses the dynamic compilation approach. For a walkthrough that demonstrates how to compile, see Walkthrough: Developing and Using a Custom Web Server Control.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Security.Permissions;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Samples.AspNet.CS.Controls
{

    // This code snippet creates a simple Web Part control.
    [AspNetHostingPermission(SecurityAction.Demand,
      Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand,
      Level = AspNetHostingPermissionLevel.Minimal)]
    public class SimpleControl : WebPart
    {

        private String _text = "Simple control text";

        public string Text
        {
            get
            {
                if (_text != null)
                    return _text;
                else
                    return string.Empty;
            }
            set { _text = value; }
        }

        protected override void Render(System.Web.UI.HtmlTextWriter 
      writer)
        {
            writer.Write(this.Text);
        }
    }
}
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Collections.Generic
Imports System.Security.Permissions
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

' This code snippet creates a simple Web Part control.
Namespace Samples.AspNet.VB.Controls

  <AspNetHostingPermission(SecurityAction.Demand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  <AspNetHostingPermission(SecurityAction.InheritanceDemand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  Public Class SimpleControl
    Inherits System.Web.UI.WebControls.WebParts.WebPart

    Private _text As String = "Simple control text"

    Public Property [Text]() As String
      Get
        If Not (_text Is Nothing) Then
          Return _text
        Else
          Return String.Empty
        End If
      End Get
      Set(ByVal value As String)
        _text = value
      End Set
    End Property

    Protected Overrides Sub Render(ByVal writer _
      As System.Web.UI.HtmlTextWriter)

      writer.Write(Me.Text)

    End Sub

  End Class

End Namespace

The second part of the example contains source code to create the custom zone and the custom WebPartVerb object. Note that the zone overrides the OnCreateVerbs method to add the custom verb to any WebPart controls in the zone. The verb is added by default to the verbs menu of the controls. Note that, in the code for the custom verb, the constructor for the verb uses a server click handler, and that the method called creates a complete copy of any control that inherits directly from the WebPart class, adding the newly created copy to the same zone. Like the first part of the code example, this source code must be compiled, and in this example the source file was placed in an App_Code subfolder to be dynamically compiled.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Security.Permissions;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Samples.AspNet.CS.Controls
{
/* 
This code sample creates a Web Part zone and adds the 
"Copy Web Part" verb to any control in the zone.
*/
[AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
public class ZoneWithAddedVerb : WebPartZone
{

  protected override void OnCreateVerbs(WebPartVerbsEventArgs e)
  {
    List<WebPartVerb> newVerbs = new List<WebPartVerb>();
    newVerbs.Add(new CopyWebPartVerb(CopyWebPartToNewOne));
    e.Verbs = new WebPartVerbCollection(e.Verbs,newVerbs);
    base.OnCreateVerbs(e);
  }

  void CopyWebPartToNewOne(object sender, WebPartEventArgs e)
  {
    WebPartManager wpmgr = 
      WebPartManager.GetCurrentWebPartManager(Page);
    System.Web.UI.WebControls.WebParts.WebPart wp;
    Type tp = e.WebPart.GetType(); 
    wp = (System.Web.UI.WebControls.WebParts.WebPart)Activator.CreateInstance(tp);   
    wpmgr.AddWebPart(wp, e.WebPart.Zone, e.WebPart.ZoneIndex + 1);
  }
}
  [AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  internal class CopyWebPartVerb : WebPartVerb
  {
    private const String _copyWebPartImageUrl = "~/CopyVerb.ico";

    internal CopyWebPartVerb(WebPartEventHandler serverClickHandler) :  
       base("MyVerb", serverClickHandler)
    { }
    public override string Text
    {
      get { return "Copy Web Part"; }
      set { ;}
    }
    public override string Description
    {
      get { return "This verb will copy this web part control " +
        "to a new one below"; }
      set { ; }
    }
    public override bool Enabled
    {
      get { return base.Enabled; }
      set { base.Enabled = value; }
    }
    
    public override string ImageUrl
    {
      get { return _copyWebPartImageUrl; }
      set { ; }
    }
  }
}
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Security.Permissions
Imports System.Collections.Generic
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

' This code sample creates a Web Part zone and adds the 
' "Copy Web Part" verb to any control in the zone.
Namespace Samples.AspNet.VB.Controls

  <AspNetHostingPermission(SecurityAction.Demand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  <AspNetHostingPermission(SecurityAction.InheritanceDemand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  Public Class ZoneWithAddedVerb
    Inherits WebPartZone

    'public class ExtendedWebPartZoneBase 
    Protected Overrides Sub OnCreateVerbs(ByVal e _
      As WebPartVerbsEventArgs)

      Dim newVerbs As List(Of WebPartVerb) = _
        New List(Of WebPartVerb)
      newVerbs.Add(New CopyWebPartVerb(AddressOf CopyWebPartToNewOne))
      e.Verbs = New WebPartVerbCollection(e.Verbs, newVerbs)
      MyBase.OnCreateVerbs(e)

    End Sub


    Sub CopyWebPartToNewOne(ByVal sender As Object, _
      ByVal e As WebPartEventArgs)

      Dim wpmgr As WebPartManager = _
        WebPartManager.GetCurrentWebPartManager(Page)
      Dim wp As System.Web.UI.WebControls.WebParts.WebPart
      Dim tp As Type = e.WebPart.GetType()
      wp = CType(Activator.CreateInstance(tp), _
        System.Web.UI.WebControls.WebParts.WebPart)
      wpmgr.AddWebPart(wp, e.WebPart.Zone, e.WebPart.ZoneIndex + 1)

    End Sub

  End Class


  <AspNetHostingPermission(SecurityAction.Demand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  <AspNetHostingPermission(SecurityAction.InheritanceDemand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  Friend Class CopyWebPartVerb
    Inherits WebPartVerb
    Private Const _copyWebPartImageUrl As String = "~/CopyVerb.ico"

    Friend Sub New(ByVal serverClickHandler As WebPartEventHandler)
      MyBase.New("MyVerb", serverClickHandler)

    End Sub

    Public Overrides Property [Text]() As String
      Get
        Return "Copy Web Part"
      End Get
      Set(ByVal value As String)
      End Set
    End Property

    Public Overrides Property Description() As String
      Get
        Return "This verb will copy this web part control to a " _
               & "new one below"
      End Get
      Set(ByVal value As String)
      End Set
    End Property

    Public Overrides Property Enabled() As Boolean
      Get
        Return MyBase.Enabled
      End Get
      Set(ByVal value As Boolean)
        MyBase.Enabled = value
      End Set
    End Property
    
    Public Overrides Property ImageUrl() As String
      Get
        Return Me._copyWebPartImageUrl
      End Get
      Set(ByVal value As String)
      End Set
    End Property

  End Class

End Namespace

The third part of the code example is the Web page that hosts the controls. Note that there is a Register directive near the top of the page to declare the namespace of the custom controls. No assembly is declared because this example uses dynamic compilation. The custom WebPart control is declared within the custom zone. You could also declare other WebPart controls in this zone, and the custom verb would be added to their verbs menus as well.

<%@ Page Language="C#" %>
<%@ Register TagPrefix="verbsample" 
    namespace="Samples.AspNet.CS.Controls" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:WebPartManager ID="WebPartManager1" runat="server" />
    <verbsample:ZoneWithAddedVerb id="ZoneWithAddedVerb1" 
      HeaderText="Zone with Added Verb" runat="server">
        <ZoneTemplate>
           <verbsample:SimpleControl id="SimpleControl1" 
            title="Simple Control" runat="server" /> 
        </ZoneTemplate>
        </verbsample:ZoneWithAddedVerb>
     </form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ Register TagPrefix="verbsample" 
    namespace="Samples.AspNet.VB.Controls" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:WebPartManager ID="WebPartManager1" runat="server" />
    <verbsample:ZoneWithAddedVerb id="ZoneWithAddedVerb1" 
      HeaderText="Zone with Added Verb" runat="server">
        <ZoneTemplate>
           <verbsample:SimpleControl id="SimpleControl1" 
            title="Simple Control" runat="server" /> 
        </ZoneTemplate>
        </verbsample:ZoneWithAddedVerb>
     </form>
</body>
</html>

Load the page in a browser, and click the verbs menu on the custom WebPart control. The Copy Web Part verb should be visible. Click the verb, and note that a copy of the control is added to the zone.

Remarks

Web Parts verbs are interactive UI elements, typically represented by buttons or hyperlinks, that appear in pages that use Web Parts controls. Users click verbs to carry out common UI actions for Web Parts controls, such as closing a WebPart control or closing the edit display mode (a special page view associated with the EditorZone control). All standard verbs provided with the Web Parts control set are associated with either a Web Parts zone (WebPartZone, EditorZone, CatalogZone, or ConnectionsZone) or a Web Parts Part control (WebPart, GenericWebPart, EditorPart, or CatalogPart).

When the standard verbs are associated with a zone, their actions apply at the zone level, and the verbs usually appear in the zone's header or footer area. The following table lists the common zone-level verbs and describes their actions.

Zone-level verb Action
Close verb Used with zones that inherit from the ToolZone class. Hides the UI for a zone and typically returns the page to its normal browse mode view.
Apply verb Used with zones that inherit from the EditorZoneBase class. Applies changes that a user has made.
OK verb Used with zones that inherit from the EditorZoneBase class. Has the combined effect of the apply and close verbs; it applies changes and hides the zone's UI.
Cancel verb Used with zones that inherit from the ToolZone class. Cancels any pending changes a user has made.
Add verb Used with zones that inherit from the CatalogZoneBase class. Adds a control that a user has selected from a catalog of controls to a specified WebPartZone zone.
Configure verb Used with zones that inherit from the ConnectionsZone class. Opens a view to enable users to configure existing connections.
Disconnect verb Used with zones that inherit from the ConnectionsZone class. Terminates an existing connection between two controls.

As for the standard verbs that are associated with Part controls, their actions apply to the control itself (or to its child controls). The part control acts as a container for the verb and manages the rendering of the verb. Most standard verbs associated with part controls appear either directly in the title bar or in a drop-down verbs menu within the title bar. Whether these verbs appear directly in the header or in a verbs menu is determined by the WebPartVerbRenderMode property; by default, the verbs appear in a verbs menu. The following table lists common verbs associated with the various Part controls and describes their actions.

Part control verb Action
Minimize verb Appears in the verbs menu of each server control contained in a WebPartZoneBase zone. Reduces the control to a minimal representation, hiding everything in its UI except the restore verb.
Restore verb Appears in the verbs menu of each server control contained in a WebPartZoneBase zone. Returns a minimized control to its normal size and view.
Close verb Appears in the verbs menu of each server control contained in a WebPartZoneBase zone. Closes a control and adds it to the page catalog, which means the control is removed from the page but, if there is a properly designed catalog, users can reopen the closed control and return it to the page.
Connect verb Appears in the verbs menu of a control contained in a WebPartZoneBase zone, if the page is in connect display mode and if the control can form connections. Opens a connection UI so that users can create a connection between controls.
Delete verb Appears in the verbs menu of a control contained in a WebPartZoneBase zone, if the control was added to the page programmatically (rather than being declared in persistence format). Permanently deletes the control instance and any associated personalization data so that, unlike a closed control, the deleted instance can never be restored to the page.
Edit verb Appears in the verbs menu of a control contained in a WebPartZoneBase zone, if the page is designed to permit editing of controls and if the page is in edit display mode. Selects the control for editing and opens the editing UI.
Export verb Appears in the verbs menu of a control contained in a WebPartZoneBase zone, if the control and application are enabled for export. Opens a dialog box that enables users to export a description file that contains the serialized state of the control. For details, see the ExportMode property.
Help verb Appears in the verbs menu of a control contained in a WebPartZoneBase zone, if the control is designed to provide a Help file. Launches a page that provides help for the control.

Along with the standard verbs that are provided with the Web Parts control set, you can also create custom verbs. A common scenario for creating custom verbs is to create verbs associated with Part controls. When you create these verbs, by default they will appear on the verbs menu along with the standard verbs. The basic approach for creating such verbs is to inherit from the WebPartVerb class to create one or more custom verbs. Then create a control that inherits from the WebPart base class and add the custom WebPartVerb objects to a WebPartVerbCollection collection. This collection can then be added to the Verbs collection of the control, which will cause the custom verbs to appear automatically on the control's verbs menu at run time. As with the standard verbs, you can access the custom verbs programmatically on a WebPart control.

Web Parts applications can use server controls that do not inherit from the WebPart class, such as custom controls, user controls, or ASP.NET controls. These server controls, if added to a WebPartZoneBase zone, are dynamically wrapped with a GenericWebPart object at run time, which enables them to function as run-time WebPart controls. To add custom verbs to a server control that does not inherit from the WebPart class, the server control must implement the IWebActionable interface and override its Verbs property.

When you create a verb, you can create two types of associated event handlers that will run when a user clicks the verb: a server-side event handler (code that runs on the server), and a client-side event handler (code that runs in the client browser). Also, you can define custom states for verbs, which is a useful way to provide visual cues to users. For example, you could create a state indicating that a verb has been selected, and then provide appropriate changes in the UI to notify the user that the verb is already selected. Another useful behavior is the ability to disable verbs; by doing this, you can prevent users from clicking verbs if doing so would be harmful or ineffective based on the state of the application.

The WebPartVerb class does not expose any unique methods; its exposed methods are all overrides of base methods. It does however contain a number of properties for developers to be aware of. The Checked property indicates whether a verb is currently selected. The ClientClickHandler and ServerClickHandler properties refer to the respective handlers within the class (if any exist) for client and server click events. The Description property contains text that describes the purpose of a verb in a ToolTip when users position a mouse pointer over the verb in the UI. The Enabled property indicates the current status of a verb, which determines whether a user can click the verb and execute its action. The ID property provides a unique ID for the verb object. The ImageUrl property contains a URL to an image that can be used to represent a verb in the UI in place of the default rendering (typically a button or a hyperlink). The Text property contains the label text that appears directly on the verb in the UI. The overridden Visible property determines whether a verb is currently displayed in the UI. Verbs are often hidden or disabled at different times in a Web Parts application; for example, specialty verbs (such as the edit verb and the connect verb) are displayed only when the appropriate controls, settings, and display modes exist on the page to enable those verbs to take meaningful action.

Other key members for working with verbs include the WebPartZoneBase.OnCreateVerbs method, which is an event handler that can be overridden for custom handling of the verb creation process, and the WebPartZoneBase.CreateVerbs event.

Constructors

WebPartVerb(String, String)

Initializes a new instance of the WebPartVerb class and associates a client-side click event handler with the instance.

WebPartVerb(String, WebPartEventHandler, String)

Initializes a new instance of the WebPartVerb class and associates both client and server-side click event handlers with the instance.

WebPartVerb(String, WebPartEventHandler)

Initializes a new instance of the WebPartVerb class and associates a server-side click event handler with the instance.

Properties

Checked

Gets or sets a value indicating that some state associated with a custom verb is currently active or selected.

ClientClickHandler

Gets the string containing the method name of the client-side event handler defined in the WebPartVerb constructor.

Description

Gets or sets a short description of the verb.

Enabled

Gets or sets a value that indicates whether a verb is enabled.

ID

Gets a string containing a unique ID for a verb.

ImageUrl

Gets or sets a string containing a URL to an image that represents a verb in the user interface (UI).

IsTrackingViewState

Gets a value that indicates whether view state is currently being tracked for a verb.

ServerClickHandler

Gets a reference to the method that handles server-side click events for the verb.

Text

Gets or sets the text label for a verb that is displayed in the user interface (UI).

ViewState

Gets a dictionary of state information that allows you to save and restore the view state of a server control across multiple requests for the same page.

Visible

Gets or sets a value that indicates whether a verb is visible to users.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
LoadViewState(Object)

Restores view-state information from a previous page request that was saved by the SaveViewState() method.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
SaveViewState()

Saves a WebPartVerb object's view-state changes that occurred since the page was last posted back to the server.

ToString()

Returns a string that represents the current object.

(Inherited from Object)
TrackViewState()

Tracks view-state changes to a verb so the changes can be stored in the verb's StateBag object.

Explicit Interface Implementations

IStateManager.IsTrackingViewState

Implements the IsTrackingViewState property by calling the WebPartVerb class's own IsTrackingViewState property.

IStateManager.LoadViewState(Object)

Implements the LoadViewState(Object) method of the IStateManager interface by calling the WebPartVerb class's own LoadViewState(Object) method.

IStateManager.SaveViewState()

Implements the SaveViewState() method by calling the WebPartVerb class's own SaveViewState() method.

IStateManager.TrackViewState()

Implements the TrackViewState() method by calling the WebPartVerb class's own TrackViewState() method.

Applies to

See also