New to Telerik UI for ASP.NET AJAXStart a free 30-day trial

Adding and Editing Templates at Runtime

You can also add templates to RadToolBar at runtime, using the ItemTemplate property of the RadToolBarButtons and RadToolBarSplitButtons. This property is of type ITemplate, so you must assign an object that implements that interface as a value:

Only RadToolBarButton and RadToolBarSplitButton support templates. If you set a template to a RadToolBarSplitButton, this template will not be inherited by the child buttons of the split button. If you want to define templates for the child buttons, you should define an ItemTemplate for each child button.

The buttons (split buttons) should be dynamically added so that templates can be defined at run time. Also, the buttons (split buttons) should be bound to be able to eval DataBinder expressions. In other words, you should call the DataBind method of the RadToolBar object or bind the buttons (split buttons) that are about to use DataBinder.Eval . You can bind a specific button (split button) by calling the DataBind method of this specific button (split button).

NoteThe template has to be instantiated for each button (split button) upon a postback. Since the TextBoxTemplate class initializes the label on InstantiateIn we called the InstantiateIn method of the TextBoxTemplate object for each button (split button).
C#
protected void Page_Load(object sender, EventArgs e){ 
    if (!Page.IsPostBack) 
    {
        RadToolBar1.Items.Add(new RadToolBarButton("Button1"));
        RadToolBar1.Items.Add(new RadToolBarButton("Button2"));
    } 
    TextBoxTemplate template = new TextBoxTemplate(); 
    foreach (RadToolBarItem item in RadToolBar1.Items) 
    {  
        if(item is RadToolBarButton)      
            template.InstantiateIn(item); 
    } 
    RadToolBar1.DataBind();
}

class TextBoxTemplate : ITemplate
{
    public void InstantiateIn(Control container) 
    {     
        Label label1 = new Label();   
        label1.ID = "ItemLabel";
        label1.Text = "Text";  
        label1.Font.Size = 10;   
        label1.Font.Bold = true;  
        label1.DataBinding += new EventHandler(label1_DataBinding);  
        container.Controls.Add(label1);
    }

    private void label1_DataBinding(object sender, EventArgs e) 
    {  
        Label target = (Label)sender;  
        RadToolBarButton button = (RadToolBarButton)target.BindingContainer; 
        string buttonText = (string)DataBinder.Eval(button, "Text"); 
        target.Text = buttonText; 
    }
}				

The end result of this code looks like the following:

ToolBar Template

Not finding the help you need?
Contact Support