HOW-TO Create dynamic Tabs and adding templates to them at run time (v1.x only)SOLUTIONYou can dynamically create tabs and add them to the RadTabStrip. But you can also load dynamic templates from user controls (.ascx) and add them to these tabs at run time. The controls in the user control persist their state correctly and may have their own server-side events. For example consider the following set-up - an .aspx file with the following declaration:
<%@ Register TagPrefix="radTS" Namespace="Telerik.WebControls" Assembly="RadTabStrip" %>...<radTS:RadTabStrip id=RadTabStrip1 Runat="server" Width="400" Height="300"></radTS:RadTabStrip>
In the code behind we have the following code:...protected Telerik.WebControls.RadTabStrip RadTabStrip1;protected Telerik.WebControls.Tab Tab1;protected System.Web.UI.WebControls.TextBox TextBox1;...private void Page_Load(object sender, System.EventArgs e){ if (!Page.IsPostBack) { Tab1 = new Tab(); Tab1.ID = "Tab1"; Tab1.Text = "Tab 1"; RadTabStrip1.TabCollection.Add(Tab1); } else { Tab1 = RadTabStrip1.TabCollection["Tab1"]; } ITemplate aTemplate = Page.LoadTemplate("Tab1.ascx"); aTemplate.InstantiateIn(Tab1.TabTemplate); TextBox1 = (TextBox)RadTabStrip1.FindControl("TextBox1"); TextBox1.Text = "Set from code-behind";}
ITemplate aTemplate = Page.LoadTemplate("Tab1.ascx"); aTemplate.InstantiateIn(Tab1.TabTemplate);
TextBox1 = (TextBox)RadTabStrip1.FindControl("TextBox1"); TextBox1.Text = "Set from code-behind";}
You see that we create a new tab when the page loads for the first time and we add it to the RadTabStrip's TabCollection. If the page has been posted back we simply get a reference to the already created tab. Then using the Page.LoadTemplate method we load a user control and we instantiate it in the Tab's TabTemplate.You see we use the RadTabStrip's FindControl method to get a reference to a control inside the .ascx user control, which now is part of the RadTabStrip's control hierarchy and set some of its properties at run time.Here is the source of the .ascx file along with its code-behind:
[Tab1.ascx]<%@ Control Language="c#" AutoEventWireup="false" Codebehind="Tab1.ascx.cs" Inherits="TabStripTest.Tab1"%><asp:button runat="server" id="Button1" Text="Fire event" onclick="Button1_Click" /><asp:Panel ID="Panel1" Runat="server"><asp:TextBox Runat="server" ID="TextBox1" Text="Initial text"></asp:TextBox></asp:Panel> [Tab1.ascx.cs]...protected void Button1_Click(object sender, EventArgs e){ Response.Write("Event fired");}...
Resources Buy Try