Tuesday, April 21, 2009

Creating Custom Toolbar and ToolBarbutton

Hi All,

Many times we may require creating a custom toolbar, just like a toolbar that you can see when you click on View Item link of any item in list or document library and on DispForm.aspx.

Here we will also achieve the same thing. We will create one custom toolbar and also create custom toolbar button and add the buttons to the toolbar and set the different URLs to them.

First we need to create a ToolBarbutton. ToolBarButton class is available in Microsoft.Sharepoint.WebControls. ToolBarButton.

Here go to 12 hives control templates and observe one ascx control which is ToolBarButton.ascx. We will use this.

So first for an example in webpart we need to render the toolbar with toolbarbuttons in it with different URLs.

So we will proceed like this.

ToolBarButton toolbuttonNew =
(ToolBarButton)Page.LoadControl("~/_controltemplates/ToolBarButton.ascx");
toolbuttonNew.Text = "New";
toolbuttonNew.ImageUrl = "/_layouts/images/customimage.GIF"; //choose your own image url
toolbuttonNew.NavigateUrl = {url where you want to navigate on click};



Same way, you can have as many ToolBar buttons as you want.

Now let’s say you are done with the buttons, now get the reference of the ToolBat itself. Same again in 12 hive’s control template, you can observe ToolBar.ascx. so we will use this.

ToolBar toolbar = (ToolBar)Page.LoadControl("~/_controltemplates/ToolBar.ascx");


Now, add button to it

toolbar.Buttons.Controls.Add(toolbuttonNew);


and finally add toolbar to the webpart.

Controls.Add(toolbar);


That’s it. You are done with your job.

1 comment:

Tony Bierman said...

Short and sweet. Thank for the succinct and useful post.




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com