Binding to Object-Based Data Sources
You can bind RadTabStrip to an object-based data source such as ObjectDataSource, LinqDataSource or any class that implements the IEnumerable interface.
Binding to ObjectDataSource
To see this type of data binding in action, please visit the Declarative Data Sources live demo.
When using ObjectDataSource, you can bind RadTabStrip declaratively at design time. The SelectMethod of the ObjectDataSource should return an object that supports the IEnumerable or ICollection interface. Such collections include Array, ArrayList, and List
If the SelectMethod returns a collection of strings, those strings are automatically mapped to the Text property of the respective tabs. If the collection contains objects (as opposed to string values), you can use the DataTextField, DataNavigateUrlField, and the DataValueField properties to map a property from the object directly to the Text, NavigateUrl and Value properties of RadTab. If the DataFieldID and DataFieldParentID properties are set, RadTabStrip will create a hierarchy of Tabs, determining the Root ones using the following algorithm:
- their DataFieldParentID property must be null if it is of nullable (e.g. int? ) or reference (e.g. string ) type.
-example:-
ID ParentID 1 (null) 2 1 their DataFieldParentID property must return the default value if it is value type (e.g. 0 for int , Guid.Empty for Guid ).
-example:-
ID ParentID 1 0 2 1
To map additional properties from the object to other properties of the respective tab, use an TabDataBound event handler. The event arguments passed to the event, e.Tab and e.Tab.DataItem, hold the instance of the tab being bound and the DataItem associated with the tab. You can map a property from the DataItem to the property of the RadTab class (make sure to cast the DataItem object to your respective data type first).
The following example shows a RadTabStrip bound declaratively to an ObjectDataSource:
<telerik:RadTabStrip
ID="RadTabStrip1"
runat="server"
DataSourceID="ObjectDataSource1">
</telerik:RadTabStrip>
<asp:ObjectDataSource
ID="ObjectDataSource1"
runat="server"
SelectMethod="GetSiteData"
TypeName="SiteDataItem">
</asp:ObjectDataSource>
The ObjectDataSource component is configured to use the SiteDataItem business object. The SiteDataItem class is defined below:
using System.Collections.Generic;
public class SiteDataItem
{
private string _text;
private string _url;
private int _id;
private int _parentId;
public string Text
{
get { return _text; }
set { _text = value; }
}
public string Url
{
get { return _url; }
set { _url = value; }
}
public int ID
{
get { return _id; }
set { _id = value; }
}
public int ParentID
{
get { return _parentId; }
set { _parentId = value; }
}
public SiteDataItem(int id, int parentId, string text, string url)
{
_id = id;
_parentId = parentId;
_text = text;
_url = url;
}
public static List<SiteDataItem> GetSiteData()
{
List<SiteDataItem> siteData = new List<SiteDataItem>();
siteData.Add(new SiteDataItem(1, 0, "All Sites", ""));
siteData.Add(new SiteDataItem(2, 1, "Search Engines", ""));
siteData.Add(new SiteDataItem(3, 1, "News Sites", ""));
siteData.Add(new SiteDataItem(4, 2, "Yahoo", "http://www.yahoo.com"));
siteData.Add(new SiteDataItem(5, 2, "MSN", "http://www.msn.com"));
siteData.Add(new SiteDataItem(6, 2, "Google", "http://www.google.com"));
siteData.Add(new SiteDataItem(7, 3, "CNN", "http://www.cnn.com"));
siteData.Add(new SiteDataItem(8, 3, "BBC", "http://www.bbc.co.uk"));
siteData.Add(new SiteDataItem(9, 3, "FOX", "http://www.foxnews.com"));
return siteData;
}
}
Binding to an object that supports IEnumerable
To see this type of data binding in action, please visit the Hierarchical Data Binding live demo.
Instead of going through an ObjectDataSource, you can bind RadTabStrip directly to any collection that implements the ICollection or IEnumerable interface.Mapping properties of the objects in the collection to properties of the tabs works exactly the same way as when using ObjectDataSource.
To bind directly to a collection, you must bind the tabstrip at runtime, using the following steps:
-
Create the collection.
-
Add items to the collection. Depending on the type of the collection, these can be strings, or more complex objects with properties that provide data for the various properties of RadTab and/or for the ID -> ParentID relationship.
-
Set the DataSource property of RadTabStrip to the instance of the collection.
-
Call the DataBind method.
The following example shows a tabstrip bound to an ArrayList. The list contains SiteDataItem objects with the same definition as in the ObjectDataSource example, only that their ParentID property is of nullable type - int?:
private ArrayList GenerateSiteData()
{
ArrayList siteData = new ArrayList();
siteData.Add(new SiteDataItem(1, null, "All Sites", ""));
siteData.Add(new SiteDataItem(2, 1, "Search Engines", ""));
siteData.Add(new SiteDataItem(3, 1, "News Sites", ""));
siteData.Add(new SiteDataItem(4, 2, "Yahoo", "http://www.yahoo.com"));
siteData.Add(new SiteDataItem(5, 2, "MSN", "http://www.msn.com"));
siteData.Add(new SiteDataItem(6, 2, "Google", "http://www.google.com"));
siteData.Add(new SiteDataItem(7, 3, "CNN", "http://www.cnn.com"));
siteData.Add(new SiteDataItem(8, 3, "BBC", "http://www.bbc.co.uk"));
siteData.Add(new SiteDataItem(9, 3, "FOX", "http://www.foxnews.com"));
return siteData;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
RadTabStrip1.DataTextField = "Text";
RadTabStrip1.DataNavigateUrlField = "Url";
RadTabStrip1.DataFieldID = "ID";
RadTabStrip1.DataFieldParentID = "ParentID";
RadTabStrip1.DataSource = GenerateSiteData();
RadTabStrip1.DataBind();
}
}