How can I add custom Verbs to WebParts?

Last post 09-30-2009 10:11 PM by vinz. 25 replies.

Sort Posts:

  • How can I add custom Verbs to WebParts?

    02-10-2005, 3:02 PM
    • Participant
      825 point Participant
    • MrBuzzcut
    • Member since 05-25-2004, 2:12 PM
    • Posts 165
    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!
  • Re: How can I add custom Verbs to WebParts?

    02-10-2005, 3:27 PM
    Answer
    • All-Star
      29,644 point All-Star
    • Fredrik N
    • Member since 06-22-2002, 5:03 AM
    • Sweden
    • Posts 5,334
    • TrustedFriends-MVPs
    /Fredrik Normén - fredrikn @ twitter

    ASPInsider

    Microsoft MVP, MCSD, MCAD, MCT

    ASPInsiders
    My Blog
  • Re: How can I add custom Verbs to WebParts?

    02-10-2005, 3:36 PM
    • Participant
      825 point Participant
    • MrBuzzcut
    • Member since 05-25-2004, 2:12 PM
    • Posts 165
    Perfect, thanks!
  • Re: How can I add custom Verbs to WebParts?

    02-10-2005, 3:54 PM
    • Participant
      825 point Participant
    • MrBuzzcut
    • Member since 05-25-2004, 2:12 PM
    • Posts 165
    Worked great ...!
  • Re: How can I add custom Verbs to WebParts?

    02-10-2005, 4:28 PM
    • Participant
      825 point Participant
    • MrBuzzcut
    • Member since 05-25-2004, 2:12 PM
    • Posts 165
    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!
  • Re: How can I add custom Verbs to WebParts?

    02-10-2005, 4:55 PM
    • All-Star
      29,644 point All-Star
    • Fredrik N
    • Member since 06-22-2002, 5:03 AM
    • Sweden
    • Posts 5,334
    • TrustedFriends-MVPs
    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.
    /Fredrik Normén - fredrikn @ twitter

    ASPInsider

    Microsoft MVP, MCSD, MCAD, MCT

    ASPInsiders
    My Blog
  • Re: How can I add custom Verbs to WebParts?

    02-10-2005, 8:25 PM
    • Participant
      825 point Participant
    • MrBuzzcut
    • Member since 05-25-2004, 2:12 PM
    • Posts 165
    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!
  • Re: How can I add custom Verbs to WebParts?

    06-29-2005, 2:47 PM
    • Participant
      931 point Participant
    • EdenMachine
    • Member since 09-19-2002, 2:37 PM
    • Dallas
    • Posts 190
    I think the Maximize Verb would only show if the WebPart had been minimized first.
    Rich
    http://www.DevAndDesign.com/
  • Re: How can I add custom Verbs to WebParts?

    06-30-2005, 9:31 AM
    • Participant
      931 point Participant
    • EdenMachine
    • Member since 09-19-2002, 2:37 PM
    • Dallas
    • Posts 190

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

    <%@ Page Language="C#" %>

     

    <%@ Register TagPrefix="sample" Namespace="Microsoft.Sample.WebParts" %>

     

    <script runat="server">

       

        public void QuickVerbs(object sender, WebPartVerbsEventArgs e)

        {

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

     

            WebPartVerb DesignVerb = new WebPartVerb("Design Mode", new WebPartEventHandler(DesignData));

            DesignVerb.Text = "Design";

            WebPartVerb EditVerb = new WebPartVerb("Edit Mode", new WebPartEventHandler(EditData));

            EditVerb.Text = "Edit";

            WebPartVerb ConnectVerb = new WebPartVerb("Connect Mode", new WebPartEventHandler(ConnectMode));

            ConnectVerb.Text = "Connect";

            WebPartVerb BrowseVerb = new WebPartVerb("Browse Mode", new WebPartEventHandler(BrowseMode));

            BrowseVerb.Text = "Browse";

     

            WebPartVerb[] newVerbs;

           

            switch (wpm1.DisplayMode.Name)

            {

                case "Design":

                    newVerbs = new WebPartVerb[] { BrowseVerb, EditVerb, ConnectVerb, };

                    break;

                case "Edit":

                    newVerbs = new WebPartVerb[] { BrowseVerb, DesignVerb, ConnectVerb };

                    break;

                case "Connect":

                    newVerbs = new WebPartVerb[] { BrowseVerb, DesignVerb, EditVerb };

                    break;

                default:

                    newVerbs = new WebPartVerb[] { DesignVerb, EditVerb, ConnectVerb };

                    break;

            }

            e.Verbs = new WebPartVerbCollection(newVerbs);

        }

     

     

        public void DesignData(object sender, WebPartEventArgs args)

        {

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

            wpm1.DisplayMode = WebPartManager.DesignDisplayMode;

        }

     

        public void EditData(object sender, WebPartEventArgs args)

        {

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

            wpm1.DisplayMode = WebPartManager.EditDisplayMode;

            wpm1.BeginWebPartEditing(args.WebPart);

        }

     

        public void ConnectMode(object sender, WebPartEventArgs args)

        {

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

            wpm1.DisplayMode = WebPartManager.ConnectDisplayMode;

            wpm1.BeginWebPartConnecting(args.WebPart);

        }

     

        public void BrowseMode(object sender, WebPartEventArgs args)

        {

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

            wpm1.DisplayMode = WebPartManager.BrowseDisplayMode;

        }

       

    </script>

     

    <html>

    <head id="Head1" runat="server">

        <title>Web Part Page</title>

    </head>

    <body>

        <form id="form1" runat="server">

            <div>

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

            </div>

            <div>

                <table style="width: 100%">

                    <tr>

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

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

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

                                <ZoneTemplate>

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

                                </ZoneTemplate>

                            </asp:WebPartZone>

                        </td>

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

                            <asp:WebPartZone ID="WebPartZone2" runat="server">

                                <ZoneTemplate>

                                </ZoneTemplate>

                            </asp:WebPartZone>

                        </td>

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

                            <asp:ConnectionsZone ID="ConnectionsZone2" runat="server">

                            </asp:ConnectionsZone>

                            <asp:EditorZone ID="EditorZone1" runat="server">

                                <ZoneTemplate>

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

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

                                </ZoneTemplate>

                            </asp:EditorZone>

                        </td>

                    </tr>

                </table>

            </div>

        </form>

    </body>

    </html>

     

    Rich
    http://www.DevAndDesign.com/
  • Re: How can I add custom Verbs to WebParts?

    07-20-2005, 3:31 PM
    • Member
      40 point Member
    • millbuilder
    • Member since 07-18-2005, 7:48 PM
    • Posts 8
    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
  • Re: How can I add custom Verbs to WebParts?

    07-20-2005, 5:37 PM
    • Participant
      880 point Participant
    • JoeBerg
    • Member since 08-20-2004, 1:08 PM
    • Redmond
    • Posts 176
    Please could you provide more code/information about your scenario? Thanks!
  • Re: How can I add custom Verbs to WebParts?

    07-25-2005, 8:26 AM
    • Member
      40 point Member
    • millbuilder
    • Member since 07-18-2005, 7:48 PM
    • Posts 8
    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;

    }

    }

    }


  • Re: How can I add custom Verbs to WebParts?

    07-25-2005, 1:36 PM
    • Participant
      880 point Participant
    • JoeBerg
    • Member since 08-20-2004, 1:08 PM
    • Redmond
    • Posts 176

    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

  • Re: How can I add custom Verbs to WebParts?

    07-26-2005, 7:36 AM
    • Member
      40 point Member
    • millbuilder
    • Member since 07-18-2005, 7:48 PM
    • Posts 8

    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

     

     

  • Re: How can I add custom Verbs to WebParts?

    07-26-2005, 1:15 PM
    • Participant
      880 point Participant
    • JoeBerg
    • Member since 08-20-2004, 1:08 PM
    • Redmond
    • Posts 176
    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

Page 1 of 2 (26 items) 1 2 Next >