Description
The ToolBar class displays a toolbar on a Web Part or other control. You can add controls to the toolbar programmatically, or optionally provide a template for the toolbar which contains the controls that you require.
Usage Scenarios
You will typically use the ToolBar class to add a toolbar to a Web Part. You add buttons and other controls to the toolbar to enable users to perform the functionality that you require. Adding a toolbar to your Web Part provides an intuitive location for controls that your users need to access frequently.
The following code samples show how to create instantiate a ToolBar object in the CreateChildControls method of a Web Part. The samples also show how to invoke the RenderChildren method in the Render method of the Web Part. The RenderChildren method causes the toolbar to be rendered on your Web Part.
C# Code Sample
protected ToolBar webPartToolBar;
protected override void CreateChildControls()
{
webPartToolBar = (ToolBar)Page.LoadControl("/_controltemplates/ToolBar.ascx");
LinkButton saveButton = new LinkButton();
saveButton.Text = "Save Changes";
saveButton.Click += new EventHandler(saveButton_Click);
webPartToolBar.Buttons.Controls.Add(saveButton);
this.Controls.Add(webPartToolBar);
}
void saveButton_Click(object sender, EventArgs e)
{
LinkButton saveButton = (LinkButton)sender;
saveButton.Text = "Changes Saved";
}
protected override void Render(HtmlTextWriter writer)
{
RenderChildren(writer);
}
Visual Basic .NET Code Sample
Dim webPartToolBar As ToolBar
Protected Overrides Sub CreateChildControls()
webPartToolBar = CType(Me.Page.LoadControl("/_controltemplates/ToolBar.ascx"), ToolBar)
Dim saveButton As LinkButton = New LinkButton()
saveButton.Text = "Save Changes"
AddHandler saveButton.Click, AddressOf saveButton_Click
webPartToolBar.Buttons.Controls.Add(saveButton)
Me.Controls.Add(webPartToolBar)
End Sub
Sub saveButton_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim saveButton As LinkButton = CType(sender, LinkButton)
saveButton.Text = "Changes Saved"
End Sub
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
RenderChildren(writer)
End Sub