Tuesday, December 9, 2008

Custom Verbs in your Webparts

Here I would shoud you how I added custom verbs in my custom webpart.

Override
WebPartVerbCollection
property in your webpart class and provide the following implementation

public override WebPartVerbCollection Verbs

{

get

{

WebPartVerb verb = new WebPartVerb("ExportToList", ExportToList); //New Verb

verb.Text = "Save Data to List"; //Text for Verb

WebPartVerb[] newVerbs = new WebPartVerb[] { verb }; //Add the verb into the array

//Add the array of verbs to the collection

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

return verbs;

}

}



Here First I am declaring new verb that we want, in the next line I am assigning Text to it.
Then I am declaring an array to contain this new verb and finally add this very to the base Verbs collection and return that collection.

When we declare verb, in the constructor second argument is the Handller event (e.g. ExportToList) which will be fired when this verb is clicked from your webpart.

We need to implement that Handler in our code and we can write actions which we want in that as given below:

public void ExportToList(object sender, WebPartEventArgs e)
{
// Add the logic to export this list
}


With above two implementations, when you deploy this webpart, you would see that verb in your webpart.

Cheers,


No comments: