Home > MOSS 2007 > Web Part with Toolbar

Web Part with Toolbar

January 27th, 2008 Leave a comment Go to comments

A web part that has its own toolbar with different controls added to it programmatically is easy to build.

1) Create a web part and override CreateChildControls and Render methods:

  1. public class WPT : WebPart {
  2. protected ToolBar tlb;
  3.  
  4. protected override void CreateChildControls()
  5. {
  6. base.CreateChildControls();
  7. tlb = (ToolBar)Page.LoadControl("/_controltemplates/ToolBar.ascx");
  8. tlb.ID = "NavNodesTB";
  9. tlb.Template_Buttons = new BtnTemplate();
  10. Controls.Add(tlb);
  11. }
  12. protected override void Render(HtmlTextWriter writer)
  13. {
  14. RenderChildren(writer);
  15. }
  16. }

2) Create the BtnTemplate class that implements ITemplate interface. Add all the controls you want to show up on the toolbar

  1. public class BtnTemplate : ITemplate
  2. {
  3. DropDownList ddlNumbers;
  4. HyperLink tlBarLink;
  5.  
  6. void ITemplate.InstantiateIn(Control container)
  7. {
  8. tlBarLink = new HyperLink();
  9. tlBarLink.ID = "idClickMeText";
  10. tlBarLink.Text = "Click Me";
  11. tlBarLink.ToolTip = "Click Me";
  12. tlBarLink.NavigateUrl = "javascript:alert('Hello World!')";
  13.  
  14. ddlNumbers = new DropDownList();
  15. ddlNumbers.ID = "idNumbers";
  16. ddlNumbers.Load += new EventHandler(ddlNumbers_Load);
  17. ddlNumbers.AutoPostBack = false;
  18.  
  19. container.Controls.Add(tlBarLink);
  20. container.Controls.Add(ddlNumbers);
  21.  
  22. }
  23.  
  24. void ddlNumbers_Load(object sender, EventArgs e)
  25. {
  26. ddlNumbers.Items.Clear();
  27. ddlNumbers.Items.Add(new ListItem("One"));
  28. ddlNumbers.Items.Add(new ListItem("Two"));
  29. ddlNumbers.Items.Add(new ListItem("Three"));
  30. }
  31. }

3) Finally,deploy your web part and you are pretty much done! Here is how your web part looks like.

Source Code can be downloaded here.

Update 13/Sep/2008:   Another way of adding toolbar is now documented here.

Categories: MOSS 2007 Tags:
  1. Paul Stork
    January 28th, 2008 at 20:15 | #1

    Thank you for this example. I’ve been trying to find a way to do this in code for about 6 months. So far I’ve only been able to find descriptions of how to do it declaratively.

  2. Reza Alirezaei
    January 29th, 2008 at 11:31 | #2

    Glad to hear that.Stay tuned , I am posting more stuff in this area soon!

  3. frikouflios
    February 13th, 2008 at 07:50 | #3

    very usefull example, i was looking every where to find this !!
    thanks

  4. Viji
    March 24th, 2008 at 05:30 | #4

    thank u so much…. exactly this is what i want

  5. Tom Lambert
    June 18th, 2008 at 09:35 | #5

    Is there anyway I can see what “/_controltemplates/ToolBar.ascx” looks like?

  6. Tom Lambert
    June 18th, 2008 at 09:47 | #6

    nevermind

  7. August 6th, 2008 at 12:27 | #7

    Great post Reza, exactly the interface elements I was looking for.

  8. Reza Alirezaei
    September 13th, 2008 at 18:03 | #8

    For those who have subscribed to this post, here is another way of adding toolbars to your Web parts:

    https://blogs.devhorizon.com/reza/?p=670

  9. Arash Aghajani
    September 16th, 2008 at 05:34 | #9

    Great post…
    Thanks Reza.

  10. Hemnath S
    February 23rd, 2009 at 01:50 | #10

    Hey this an awesome post.. cool one, thanks

You must be logged in to post a comment.