locked
How can I add custom Verbs to WebParts? RRS feed

  • Question

  • User-1779164091 posted
    I have a .dll that contains several classes that inherit from WebPart. I would like to add custom Verbs to their dropdown menus. I'm a little confused: It seems like the existing Verbs are members of the WebPartZoneBase class, though the WebPart class has a read-only collection of Verbs via IWebPart. My questions, then: How can I add my own Verbs to my classes that inherit from WebPart? I'm guessing I need to create my own WebPartVerb classes and somehow associate them with a WebPArtZone. How can I determine when and which Verb on which WebPart has been clicked? This really has to do with all Verbs, not just custom ones. Thanks!
    Thursday, February 10, 2005 3:02 PM

Answers

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 10, 2005 3:27 PM
  • User-965471452 posted
    The WebPartVerbCollection constructors do not allow you to add multiple WebPartVerbs with the same ID.  The default WebPartVerbs (Minimize, Close, etc.) do not have IDs, so they cannot be added to a new WebPartVerbCollection.

    Depending on the scenario, there are other alternatives besides trying to create a new WebPartVerbCollection:

    1. To hide a default WebPartVerb (Minimize, Close, etc.), set its Visible property to false.
    2. To add a new WebPartVerb, use the WebPartZoneBase.CreateVerbs event, or the WebPart.Verbs property.

    Hope this helps,

    -Mike
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 1, 2005 9:21 PM
  • User-1034726716 posted

    But does anyone know how to add custom verbs to a webpart

    I have written an example that you might be interested with:

    Enhanced Custom WebPartZone with Custom Move Option Verbs in ASP.NET 2.0


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 30, 2009 10:11 PM

All replies

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 10, 2005 3:27 PM
  • User-1779164091 posted
    Perfect, thanks!
    Thursday, February 10, 2005 3:36 PM
  • User-1779164091 posted
    Worked great ...!
    Thursday, February 10, 2005 3:54 PM
  • User-1779164091 posted
    Quick question: When I add a Verb in code like that, I still get the "default" verbs (it seems that my Verb should be the only one since i've overridden "Verbs") -- Minimize, Close and Export -- except "MAximize" has been supplanted by my new Verb ... How can I grab those "base" Verbs and return them along with my custom Verbs? I tried accessing base.Verbs but it was empty. Thanks!
    Thursday, February 10, 2005 4:28 PM
  • User961349301 posted
    You can't get them from the WebPart. If you want them to not appear, just set the AllowMinimize, AllowClose,AllowHide and AllowEdit properties of the WebPart to false.
    Thursday, February 10, 2005 4:55 PM
  • User-1779164091 posted
    That's cool ... I'm wondering why my "Maximize" Verb disappeared when I didn't set AllowMaximize to false ... I'll have to tinker with that tomorrow ... Thanks!
    Thursday, February 10, 2005 8:25 PM
  • User-2053643153 posted
    I think the Maximize Verb would only show if the WebPart had been minimized first.
    Wednesday, June 29, 2005 2:47 PM
  • User-2053643153 posted

    Here's an example of adding Verbs to all WebParts in the WebPartZone at once:

    <%@ Page Language="C#" %><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>

    <o:p> </o:p>

    <%@ Register TagPrefix="sample" Namespace="Microsoft.Sample.WebParts" %><o:p></o:p>

    <o:p> </o:p>

    <script runat="server"><o:p></o:p>

        <o:p></o:p>

        public void QuickVerbs(object sender, WebPartVerbsEventArgs e)<o:p></o:p>

        {

            WebPartManager wpm1 = WebPartManager.GetCurrentWebPartManager(this.Page);<o:p></o:p>

    <o:p> </o:p>

            WebPartVerb DesignVerb = new WebPartVerb("Design Mode", new WebPartEventHandler(DesignData));<o:p></o:p>

            DesignVerb.Text = "Design";<o:p></o:p>

            WebPartVerb EditVerb = new WebPartVerb("Edit Mode", new WebPartEventHandler(EditData));<o:p></o:p>

            EditVerb.Text = "Edit";<o:p></o:p>

            WebPartVerb ConnectVerb = new WebPartVerb("Connect Mode", new WebPartEventHandler(ConnectMode));<o:p></o:p>

            ConnectVerb.Text = "Connect";<o:p></o:p>

            WebPartVerb BrowseVerb = new WebPartVerb("Browse Mode", new WebPartEventHandler(BrowseMode));<o:p></o:p>

            BrowseVerb.Text = "Browse";<o:p></o:p>

    <o:p> </o:p>

            WebPartVerb[] newVerbs;<o:p></o:p>

            <o:p></o:p>

            switch (wpm1.DisplayMode.Name)<o:p></o:p>

            {<o:p></o:p>

                case "Design":<o:p></o:p>

                    newVerbs = new WebPartVerb[] { BrowseVerb, EditVerb, ConnectVerb, };<o:p></o:p>

                    break;<o:p></o:p>

                case "Edit":<o:p></o:p>

                    newVerbs = new WebPartVerb[] { BrowseVerb, DesignVerb, ConnectVerb };<o:p></o:p>

                    break;<o:p></o:p>

                case "Connect":<o:p></o:p>

                    newVerbs = new WebPartVerb[] { BrowseVerb, DesignVerb, EditVerb };<o:p></o:p>

                    break;<o:p></o:p>

                default:<o:p></o:p>

                    newVerbs = new WebPartVerb[] { DesignVerb, EditVerb, ConnectVerb };<o:p></o:p>

                    break;<o:p></o:p>

            }<o:p></o:p>

            e.Verbs = new WebPartVerbCollection(newVerbs);<o:p></o:p>

    <o:p></o:p>

        }<o:p></o:p>

    <o:p> </o:p>

    <o:p> </o:p>

        public void DesignData(object sender, WebPartEventArgs args)<o:p></o:p>

        {<o:p></o:p>

            WebPartManager wpm1 = WebPartManager.GetCurrentWebPartManager(this.Page);<o:p></o:p>

            wpm1.DisplayMode = WebPartManager.DesignDisplayMode;<o:p></o:p>

        }<o:p></o:p>

    <o:p> </o:p>

        public void EditData(object sender, WebPartEventArgs args)<o:p></o:p>

        {<o:p></o:p>

            WebPartManager wpm1 = WebPartManager.GetCurrentWebPartManager(this.Page);<o:p></o:p>

            wpm1.DisplayMode = WebPartManager.EditDisplayMode;<o:p></o:p>

            wpm1.BeginWebPartEditing(args.WebPart);<o:p></o:p>

        }<o:p></o:p>

    <o:p> </o:p>

        public void ConnectMode(object sender, WebPartEventArgs args)<o:p></o:p>

        {<o:p></o:p>

            WebPartManager wpm1 = WebPartManager.GetCurrentWebPartManager(this.Page);<o:p></o:p>

            wpm1.DisplayMode = WebPartManager.ConnectDisplayMode;<o:p></o:p>

            wpm1.BeginWebPartConnecting(args.WebPart);<o:p></o:p>

        }<o:p></o:p>

    <o:p> </o:p>

        public void BrowseMode(object sender, WebPartEventArgs args)<o:p></o:p>

        {<o:p></o:p>

            WebPartManager wpm1 = WebPartManager.GetCurrentWebPartManager(this.Page);<o:p></o:p>

            wpm1.DisplayMode = WebPartManager.BrowseDisplayMode;<o:p></o:p>

        }<o:p></o:p>

        <o:p></o:p>

    </script><o:p></o:p>

    <o:p> </o:p>

    <html><o:p></o:p>

    <head id="Head1" runat="server"><o:p></o:p>

        <title>Web Part Page</title><o:p></o:p>

    </head><o:p></o:p>

    <body><o:p></o:p>

        <form id="form1" runat="server"><o:p></o:p>

            <div><o:p></o:p>

                <asp:WebPartManager ID="WebPartManager1" runat="server" /><o:p></o:p>

            </div><o:p></o:p>

            <div><o:p></o:p>

                <table style="width: 100%"><o:p></o:p>

                    <tr><o:p></o:p>

                        <td style="width: 100px; height: 100px" valign="top" align="left"><o:p></o:p>

                            <asp:WebPartZone ID="WebPartZone1" runat="server" OnCreateVerbs="QuickVerbs" CloseVerb-Visible="false"><o:p></o:p>

                                <MenuPopupStyle BorderStyle=Solid BorderWidth="1px" BackColor="White" BorderColor="Black" ShadowColor="Gray" /><o:p></o:p>

                                <ZoneTemplate><o:p></o:p>

                                    <asp:Label runat="server" title="WebPart Title" ID="lblTest" Text="This is the WebPart content right here." /><o:p></o:p>

                                </ZoneTemplate><o:p></o:p>

                            </asp:WebPartZone><o:p></o:p>

                        </td><o:p></o:p>

                        <td style="width: 100px; height: 100px" valign="top" align="left"><o:p></o:p>

                            <asp:WebPartZone ID="WebPartZone2" runat="server"><o:p></o:p>

                                <ZoneTemplate><o:p></o:p>

                                </ZoneTemplate><o:p></o:p>

                            </asp:WebPartZone><o:p></o:p>

                        </td><o:p></o:p>

                        <td style="width: 100px; height: 100px" valign="top" align="left"><o:p></o:p>

    <o:p></o:p>

                            <asp:ConnectionsZone ID="ConnectionsZone2" runat="server"><o:p></o:p>

                            </asp:ConnectionsZone><o:p></o:p>

                            <asp:EditorZone ID="EditorZone1" runat="server"><o:p></o:p>

                                <ZoneTemplate><o:p></o:p>

                                    <asp:AppearanceEditorPart runat="server" ID="AppearanceEditorPart1" /><o:p></o:p>

                                    <asp:BehaviorEditorPart runat="server" ID="BehaviorEditorPart1" /><o:p></o:p>

                                </ZoneTemplate><o:p></o:p>

                            </asp:EditorZone><o:p></o:p>

                        </td><o:p></o:p>

                    </tr><o:p></o:p>

                </table><o:p></o:p>

            </div><o:p></o:p>

        </form><o:p></o:p>

    </body><o:p></o:p>

    </html><o:p></o:p>

     

    Thursday, June 30, 2005 9:31 AM
  • User526134814 posted
    I have had a lot of trouble with verbs. I do not have any problem with adding new verbs, but I do not seem to be able to shake the existing / standard verbs.

    You write that I should just add to the constructor of my Custom webpart the following,


    this
    .AllowClose = false;
    this.AllowHide = false;
    this.AllowMinimize = false;
    this.AllowEdit = false;


    but for some reason the Verbs "Close" and "Delete" always appear !

    Does anyone have any ideas?

    Thanks in advance

    Michael
    Wednesday, July 20, 2005 3:31 PM
  • User-1545267464 posted
    Please could you provide more code/information about your scenario? Thanks!
    Wednesday, July 20, 2005 5:37 PM
  • User526134814 posted
    Hi Joe

    This is my custom WebPart.

    In it I have created a new Verb - "Edit Content"
    Even though I set the "allows" to false:

    this.AllowClose = false;
    this.AllowHide = false;
    this.AllowMinimize = false;
    this.AllowEdit = false;

    The Delete verb is still shown in the WebPart. But the "Delete" button does work. First when this.AllowClose = true; then the Close button also appears and the "Delete" button suddenly works. 

    I would appreciate any help you can give me.

    Regards


    Michael


    ---------------------------

    namespace CoreM.Web.UI.WebControls.WebParts

    {

    [DefaultProperty("Text")]

    [ToolboxData("<{0}:StaticContent runat=server></{0}:StaticContent>")]

    public class StaticContent : WebPart

    {

    #region PROPERTIES

    // Fields - Private

    bool AllowEditContent = false;

    private string _ContentContainerID = "-1";

    [Personalizable(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared), WebBrowsable]

    public string ContentContainerID

    {

    get { return _ContentContainerID; }

    set { _ContentContainerID = value; }

    }

    #endregion

    // Constructor

    public StaticContent()

    {

    // Chrome

    this.ChromeType = PartChromeType.BorderOnly;

    this.Title = "Static Element";

    this.BorderColor = System.Drawing.Color.Blue;

    this.ChromeState = PartChromeState.Normal;

    this.TitleIconImageUrl = "../Console/Image/Webparts/static.gif";

    this.CatalogIconImageUrl = "../Console/Image/Webparts/static.gif";

    this.AllowClose = false;

    this.AllowHide = false;

    this.AllowMinimize = false;

    this.AllowEdit = false;

    }

    // METHODS

    protected override void OnPreRender(EventArgs e)

    {

    // What is the current DisplayName

    WebPartManager currentConsoleWebPartmanager = WebPartManager.GetCurrentWebPartManager(this.Page);

    ConsoleWebPartManager newConsoleWebPartmanager = currentConsoleWebPartmanager as ConsoleWebPartManager;

    if (newConsoleWebPartmanager.DisplayMode.Name == "Content")

    {

    this.AllowEditContent = true;

    this.AllowClose = false;

    this.AllowHide = false;

    this.AllowMinimize = false;

    this.AllowEdit = false;

    }

    else

    {

    this.AllowClose = false;

    this.AllowHide = false;

    this.AllowMinimize = false;

    this.AllowEdit = false;

    }

    }

    protected override void CreateChildControls()

    {

    // What is the current DisplayName

    WebPartManager currentConsoleWebPartmanager = WebPartManager.GetCurrentWebPartManager(this.Page);

    Controls.Clear();

    Literal newLiteral = new Literal();

    string sContent = this.GetContent(this.ContentContainerID, currentConsoleWebPartmanager.DisplayMode.Name.ToString());

    newLiteral.Text = sContent;

    this.Controls.Add(newLiteral);

    ChildControlsCreated = true;

    }

    // METHODS

    private string GetContent(string sContentContainerID, string sDisplayMode)
    {
    return "sResult";
    }

    // VERBS

    public override WebPartVerbCollection Verbs

    {

    get

    {

    // Show the Edit content verb if the displaymode = Content

    if (this.AllowEditContent)

    {

    WebPartVerb contentVerb = new WebPartVerb("content", "NewWindow('../Console/Admin/Test.aspx?CCID=" + this.ContentContainerID + "','name','700','500','no')");

    // WebPartVerb contentVerb = new WebPartVerb("content", "onEditContent('" + this.ClientID + " ContentContainerID: " + this.ContentContainerID +"')");

    contentVerb.Text = "Edit Content";

    contentVerb.ImageUrl = "../Console/Image/Verbs/edit.gif";

    contentVerb.Description = "Edit content description";

    WebPartVerb[] newVerbs = new WebPartVerb[] { contentVerb };

    WebPartVerbCollection wp = new WebPartVerbCollection(newVerbs);

    return wp;

    }

    else

    {

    return base.Verbs;

    }

    }

    }

    // EDITOR

    public override EditorPartCollection CreateEditorParts()

    {

    List<EditorPart> customEditorPartCollection = new List<EditorPart>();

    customEditorPartCollection.Add(new CoreM.Web.UI.WebControls.WebParts.StaticContentSetup());

    EditorPartCollection editorPartCollection = new EditorPartCollection(customEditorPartCollection);

    return editorPartCollection;

    }

    }

    }


    Monday, July 25, 2005 8:26 AM
  • User-1545267464 posted

    A customer reported the same issue through the MSDN product feedback center:

    http://lab.msdn.microsoft.com/ProductFeedback/viewFeedback.aspx?feedbackid=17cbda74-5771-41f8-a61b-5828105f6639

    We fixed this for RTM. Thanks for reporting this!
    Joe

    Monday, July 25, 2005 1:36 PM
  • User526134814 posted

    Hi Joe

    Thank you for your reply.

    I think it would be a good idea with an "AllowDelete" = true/false.
    On the MSDN lab site it states that only the user who created the webpart can delete the webpart. However, how do I allow  all or  some  users permission to delete a certain webpart? One solution would be to create a custom delete verb that can be set on the webpart depending on the users permissions. In this case I would need to hide the built in "Delete" verb via something like "AllowDelete" = false.

    If "AllowDelete" = true/false is not to be implemented by MS, do you have any ideas regarding how I can implement the above mentioned scenario?

    Regards


    Michael

     

     

    Tuesday, July 26, 2005 7:36 AM
  • User-1545267464 posted
    You could create your own custom WebPartZone and add your own boolean AllowDelete property then your own WebPartChrome. In your custom WebPartChrome, you would need to re-filter the verbs collection. Something like this:

    public class CustomChrome : WebPartChrome
    {
                protected override WebPartVerbCollection FilterWebPartVerbs(WebPartVerbCollection verbs, WebPart webPart)
                {
                            WebPartVerbCollection filtered = base.FilterWebPartVerbs(verbs, webPart);
                            WebPartVerbCollection cleared = new WebPartVerbCollection();
                            customZone zone = Zone
    as customZone;
                            foreach (WebPartVerb verb in filtered)
                            {
                                        if (verb == zone.DeleteVerb)
                                        {
                                                    if (zone.AllowDelete)
                                                                cleared.Add(verb);
                                        }
                                        else
                                                    cleared.Add(verb);
                            }
                            r
    eturn cleared;
                }
    }

    I omitted the null checks for clarity; please make sure that your code is clean :). Also, I can't garantee that the code is working (I haven't tested it) but I think it demonstrates the idea.
    Hope this helps,
    Joe

    Tuesday, July 26, 2005 1:15 PM
  • User526134814 posted
    Hi Joe

    Thanks for your response.  I have tried your suggestion. However, I received the following error messages

    Error 1 'System.Web.UI.WebControls.WebParts.WebPartVerbCollection' does not contain a definition for 'Add' 
    Error 3 No overload for method 'WebPartChrome' takes '0' arguments

    Can you access an Add method in WebPartVerbCollection? Are you working with a newer version of ASP.NET?

    Regards


    Michael

    Wednesday, July 27, 2005 6:12 PM
  • User-1545267464 posted
    Actually, I should have tested that code; the WebPartVerbCollection is read-only so you need to go through the ctor to add verbs in it. You would have to do something like this instead:

    public class CustomChrome : WebPartChrome
    {
                protected override WebPartVerbCollection FilterWebPartVerbs(WebPartVerbCollection verbs, WebPart webPart)
                {
                            WebPartVerbCollection filtered = base.FilterWebPartVerbs(verbs, webPart);
                            ArrayList cleared = new ArrayList();
                            customZone zone = Zone as customZone;
                            foreach (WebPartVerb verb in filtered)
                            {
                                        if (verb == zone.DeleteVerb)
                                        {
                                                    if (zone.AllowDelete)
                                                                cleared.Add(verb);
                                        }
                                        else
                                                    cleared.Add(verb);
                            }
                            return new WebPartVerbCollection(cleared);
                }
    }

    Again, I haven't tested it :) but you get the idea. Thanks! - Joe
    Wednesday, July 27, 2005 8:16 PM
  • User-1545267464 posted
    Also, the error related to '0' parameter happens because I didn't provide the code for the constructor of the customChrome class. You would need to do something like this:

    public class CustomChrome : WebPartChrome
    {
       public CustomChrome(WebPartZoneBase zone, WebPartManager manager) : base(zone,manager) { ... }
    ...

    Hope this helps,
    Joe
    Wednesday, July 27, 2005 8:19 PM
  • User526134814 posted

    Hi Joe

    Yes I get the idea. :-) But I still feel I am working in the dark on this problem.
    I implemented using the Constructor WebPartVerbCollection(cleared); , however I received this error message:

    A WebPartVerb has already been added with ID ''.
    Parameter name: verbs

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.ArgumentException: A WebPartVerb has already been added with ID ''.
    Parameter name: verbs

    Source Error:

    Line 72:                     cleared.Add(verb);
    Line 73:             }
    Line 74:             return new WebPartVerbCollection(cleared);
    Line 75:         }
    Line 76:     }

    I would appreciate any help you can give me.

    Best regards


    Michael

    Thursday, July 28, 2005 4:13 AM
  • User-1545267464 posted
    I think this is because the verbs that you provide needs to have a unique ID. Are you adding verbs dynamically? Please let me know your exact scenario and I'll try to reproduce it.

    Thanks for raising this issue,
    Joe
    Thursday, July 28, 2005 12:01 PM
  • User-322708590 posted
    Has anyone been able to successfully filter the Delete Verb?  I tried following the suggestion above and ran into the same issues.  I get the same exception when creating the new WebPartVerbCollection with the arraylist.  I tried creating a new WebPartVerb and copying the values from the verbs in the collection into the new verb and adding that to the arraylist, but the serverClickHandler is null and i get an exception...

    thanks,

    jared

    Tuesday, October 25, 2005 2:18 PM
  • User-965471452 posted
    The WebPartVerbCollection constructors do not allow you to add multiple WebPartVerbs with the same ID.  The default WebPartVerbs (Minimize, Close, etc.) do not have IDs, so they cannot be added to a new WebPartVerbCollection.

    Depending on the scenario, there are other alternatives besides trying to create a new WebPartVerbCollection:

    1. To hide a default WebPartVerb (Minimize, Close, etc.), set its Visible property to false.
    2. To add a new WebPartVerb, use the WebPartZoneBase.CreateVerbs event, or the WebPart.Verbs property.

    Hope this helps,

    -Mike
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 1, 2005 9:21 PM
  • User1535815261 posted

    I had read all of your guy's posts related Web Parts today.

    I have learned a lot from these posts!

    Thanks alot!

    Thursday, December 29, 2005 4:41 PM
  • User-1034726716 posted

    Hi,

    Do you have any idea on how to create a custom verbs like the one in my.msn.com where you can just move a particular element to left, right, up or even down instead of drag and drop?If  you have any idea please show me some sample codes on how to work with it. thanks in advance.

    Friday, October 19, 2007 12:19 PM
  • User-1257495732 posted

    The links and tutorials were very helpful. Thanks to all here. But does anyone know how to add custom verbs to a webpart from another code like a feature receiver. I want to activate a feature and grab all the webparts in a particular url and and custom verbs in it. It seems like there is no option/method provided to add custom verbs in the WebPartVerbCollection of the current grabbed webpart. Thanks.

    Wednesday, September 30, 2009 6:29 PM
  • User-1034726716 posted

    But does anyone know how to add custom verbs to a webpart

    I have written an example that you might be interested with:

    Enhanced Custom WebPartZone with Custom Move Option Verbs in ASP.NET 2.0


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 30, 2009 10:11 PM
  • User1578243753 posted

    Thank you,

    Worked Great


    Regards


    Sandhya

    Friday, August 27, 2010 7:54 AM