Working With Tabs at the Server
Using the server-side API, you can programmatically add, remove, and edit the tabs in a tab strip.
Adding tabs on page load
After adding a RadTabStrip control to your Web page, you can use the following server-side code to add tabs when the page loads:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
RadTab tab1 = new RadTab();
tab1.Text = "Tab 1";
RadTab tab11 = new RadTab();
tab11.Text = "Child 1";
tab1.Tabs.Add(tab11);
RadTab tab2 = new RadTab();
tab2.Text = "Tab 2";
RadTab tab3 = new RadTab();
tab3.Text = "Telerik";
tab3.NavigateUrl ="https://www.telerik.com";
RadTabStrip1.Tabs.Add(tab1);
RadTabStrip1.Tabs.Add(tab2);
RadTabStrip1.Tabs.Add(tab3);
}
}
Adding tabs dynamically
You can dynamically update the Tabs collection of RadTabStrip and RadTab in response to a postback as well. Consider the following tab strip declaration:
<telerik:RadTabStrip RenderMode="Lightweight" ID="RadTabStrip1" runat="server" OnTabClick="RadTabStrip1_TabClick">
<Tabs>
<telerik:RadTab runat="server" Text="Add Root Tab" Value="R">
</telerik:RadTab>
<telerik:RadTab runat="server" Text="Add child Tab" Value="C">
</telerik:RadTab>
</Tabs>
</telerik:RadTabStrip>
The TabClick event handler adds tabs dynamically at runtime in the post-back:
protected void RadTabStrip1_TabClick(object sender, RadTabStripEventArgs e)
{
switch (e.Tab.Value)
{
case "R": // add a new root tab
RadTab RootTab = new RadTab();
RootTab.Text = "New Root Tab";
RootTab.Value = "C";
RadTabStrip1.Tabs.Add(RootTab);
break;
case "C": // add a new child tab
RadTab ChildTab = new RadTab();
RadTab ParentTab = e.Tab;
ChildTab.Text = "New ChildTab";
ParentTab.Tabs.Add(ChildTab);
break;
}
}
Clicking on both menu items results in the following:
Removing, disabling, and enabling items
To remove a tab in server-side code, use the Remove method of the RadTabCollection object that contains it. To enable or disable a tab, use the Enabled property of the RadTab object itself. The following example demonstrates these techniques.
Consider the following tab strip:
<telerik:RadTabStrip RenderMode="Lightweight" ID="RadTabStrip1" runat="server" OnTabClick="RadTabStrip1_TabClick">
<Tabs>
<telerik:RadTab runat="server" Text="Delete a Tab" Value="D">
<Tabs>
<telerik:RadTab runat="server" Text="Child 1" />
<telerik:RadTab runat="server" Text="Child 2" />
<telerik:RadTab runat="server" Text="Child 3" />
<telerik:RadTab runat="server" Text="Child 4" />
</Tabs>
</telerik:RadTab>
<telerik:RadTab runat="server" Text="Disable a Tab" Value="E">
<Tabs>
<telerik:RadTab runat="server" Text="Child 1" />
<telerik:RadTab runat="server" Text="Child 2" />
<telerik:RadTab runat="server" Text="Child 3" />
<telerik:RadTab runat="server" Text="Child 4" />
</Tabs>
</telerik:RadTab>
</Tabs>
</telerik:RadTabStrip>
The TabClick event handler deletes, disables, and enables tabs dynamically at runtime in the post-back when child tabs are clicked. It deletes children of the first root tab and disables the selected child tab of the second root tab, enabling all of its siblings:
protected void RadTabStrip1_TabClick(object sender, RadTabStripEventArgs e)
{
if (e.Tab.Owner is RadTab)
{
RadTab parentTab = (RadTab)e.Tab.Owner;
switch (parentTab.Value)
{
case "D":
parentTab.Tabs.Remove(e.Tab);
break;
case "E":
for (int i = 0; i < parentTab.Tabs.Count; i++)
{
parentTab.Tabs[i].Enabled = (parentTab.Tabs[i] != e.Tab);
}
break;
}
}
}