WSS 3.0: How to add settings sub menu for your custom web part – Verbs Explained...

Its pretty simple and straight forward. ;). The web part menu has been handled by the WebPartVerbCollection. Adding an item to the collection will solve our problem.

In your custom web part code, add the following piece of code.

        public override WebPartVerbCollection Verbs

        {

            get

            {

                //New Verb

                WebPartVerb verb = new WebPartVerb("ClientCode", "javascript:alert('hello world');");

                //Text for Verb

                verb.Text = "Helloworld";

                //Add the verb into the array

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

                //Add the array of verbs to the collection

                WebPartVerbCollection verbs = new WebPartVerbCollection(base.Verbs, newVerbs);

                //return the collection

                return verbs;

            }

        }

 

Deploy the web part and open the web site where you have the web part, click the drop down button, you will see the “helloworld” menu, click that you will see the alert window.

OK, I agree, adding the client side code is straight forward what about Server Side code. You won't believe me, its also pretty simple. The sample code is below

        public override WebPartVerbCollection Verbs

        {

            get

            {

                //New Verb with the function event handler

                WebPartVerb ServerVerb = new WebPartVerb("MysSrverCode", new WebPartEventHandler(MyFunction));

                //Text for Verb

                ServerVerb.Text = "ChangeTitle";

                //Add the verb into the array

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

                //Add the array of verbs to the collection

                WebPartVerbCollection verbs = new WebPartVerbCollection(base.Verbs, newVerbs);

                //return the collection

                return verbs;

            }

        }

        protected void MyFunction(object sender, WebPartEventArgs args)

        {

            this.Title = "New Title";

        }

Comments