Part 3: Extending your web part to include toolbars
SharePoint toolbars use user controls (.ascx files) to control HTML rendering and layout. This is great if you have an ASPX page to use them on (in fact, you can base your efforts off one of the built in pages in the layouts directory). However, this presents an interesting challenge when you want to create toolbars through assembly code alone.
You'll notice there's no constructor for the two useful classes, Toolbar and ToolbarButton in the Microsoft.SharePoint.WebControls namespace. To get toolbars in your web part, you need to create a control using the Page.LoadControl method, pointing to the relevant user control and casting the results. It’s pretty simple:
private void CreateToolbar()
ToolBarButton toolbuttonGrouping =
toolbuttonGrouping.Text = "Show in Groups";
toolbuttonGrouping.ImageUrl = "/_layouts/images/TABGEN.gif";
toolbuttonGrouping.Click += new EventHandler(toolbuttonGrouping_Click);
ToolBar toolbar= (ToolBar)Page.LoadControl("~/_controltemplates/ToolBar.ascx");
void toolbuttonGrouping_Click(object sender, EventArgs e)
throw new NotImplementedException();
This produces the following toolbar, when called from CreateChildControls:
You can add buttons to the right using Toolbar.RightButtons.Controls collection if you fancy.
P.s. Sorry for the blogging gap – it’s been rather busy lately. At your request, I’m now putting together a full downloadable sample solution for SPGridView, SPMenuField and the Toolbar stuff, complete with grouping, paging and sorting. Check back in a couple of weeks.