How can I add custom Verbs to WebParts?

Last post 10-19-2007 12:19 PM by vinz. 23 replies.

Sort Posts:

  • How can I add custom Verbs to WebParts?

    02-10-2005, 3:02 PM
    • Loading...
    • MrBuzzcut
    • Joined on 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
    • Loading...
    • Fredrik N
    • Joined on 06-22-2002, 5:03 AM
    • Sweden
    • Posts 5,333
    • Moderator
      TrustedFriends-MVPs
    /Fredrik Normén NSQUARED2
    Microsoft MVP, MCSD, MCAD, MCT

    ASPInsiders
    My Blog, ASP.Net 2.0 etc

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

    02-10-2005, 3:36 PM
    • Loading...
    • MrBuzzcut
    • Joined on 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
    • Loading...
    • MrBuzzcut
    • Joined on 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
    • Loading...
    • MrBuzzcut
    • Joined on 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
    • Loading...
    • Fredrik N
    • Joined on 06-22-2002, 5:03 AM
    • Sweden
    • Posts 5,333
    • Moderator
      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 NSQUARED2
    Microsoft MVP, MCSD, MCAD, MCT

    ASPInsiders
    My Blog, ASP.Net 2.0 etc

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

    02-10-2005, 8:25 PM
    • Loading...
    • MrBuzzcut
    • Joined on 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
    • Loading...
    • EdenMachine
    • Joined on 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
    • Loading...
    • EdenMachine
    • Joined on 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
    • Loading...
    • millbuilder
    • Joined on 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
    • Loading...
    • JoeBerg
    • Joined on 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
    • Loading...
    • millbuilder
    • Joined on 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