Adding and Editing Templates at Runtime
You can also add templates to RadTabStrip at runtime, using the TabTemplate property. This property is of type ITemplate, so you must assign an object that implements that interface as a value:
The tabs should be dynamically added so that templates can be defined at run time. Also, the tabs should be bound to be able to eval DataBinder expressions. In other words, you should call the DataBind method of the RadTabStrip object or bind the tabs that are about to use DataBinder.Eval. You can bind a specific tab by calling the DataBind method of this specific tab.
The TabTemplate should be initialized in the OnInit event of the page. This is needed as the template should be instantiated before the RadTabs are initialized.
protected override void OnInit(EventArgs e)
{
RadTabStrip1.TabTemplate = new TextBoxTemplate();
base.OnInit(e);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
RadTabStrip1.Tabs.Add(new RadTab("root1"));
RadTabStrip1.Tabs.Add(new RadTab("root2"));
}
RadTabStrip1.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;
RadTab tab = (RadTab)target.BindingContainer;
string tabText = (string)DataBinder.Eval(tab, "Text");
target.Text = tabText;
}
}
If you for some reason cannot define the template in the OnInit event of the page, you could use another approach:
The template has to be instantiated for each tab upon a postback. Since the TextBoxTemplate class initializes the label on InstantiateIn we called the InstantiateIn method of the TextBoxTemplate object for each tab.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
RadTabStrip1.Tabs.Add(new RadTab("root1"));
RadTabStrip1.Tabs.Add(new RadTab("root2"));
}
TextBoxTemplate template = new TextBoxTemplate();
foreach (RadTab tab in RadTabStrip1.GetAllTabs())
{
template.InstantiateIn(tab);
}
RadTabStrip1.DataBind();
}
The end result of this code looks like the following: