New to Telerik UI for ASP.NET AJAX? Start a free 30-day trial
Various Data Sources
You can use a wide variety of data-sources for RadTreeList structure generation. A requirement isthat these custom objects implement the ITypedList/IEnumarable /ICustomTypeDescriptor interfaces and representing self-hierarchical data. Remember to set the DataKeyNames and ParentDataKeyNames propertiesof the RadTreeList controls. Also note that the type of the fields used for DataKeyNames and ParentDataKeyNames should be the same.
The code in the next paragraph demonstrates how to:
-
use List of custom objects with Nullable properties
-
bind the RadTreeList to list of custom objects (Generic list)
-
bind RadTreeList to a DataReader
C#
<telerik:RadScriptManager ID="RadScriptManager1" runat="server" />
<b>Binding RadTreeList to:</b>
<h3>List of custom objects with <b>Nullable</b> properties</h3>
<telerik:RadAjaxPanel ID="RadAjaxPanel1" LoadingPanelID="RadAjaxLoadingPanel1" runat="server">
<telerik:RadTreeList RenderMode="Lightweight" runat="server" ID="RadTreeList1" DataKeyNames="ID" ParentDataKeyNames="PID"
OnNeedDataSource="RadTreeList1_NeedDataSource" AutoGenerateColumns="false">
<Columns>
<telerik:TreeListBoundColumn DataField="Text" HeaderText="Text" UniqueName="Text">
</telerik:TreeListBoundColumn>
<telerik:TreeListBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID">
</telerik:TreeListBoundColumn>
<telerik:TreeListBoundColumn DataField="PID" HeaderText="PID" UniqueName="PID">
</telerik:TreeListBoundColumn>
</Columns>
</telerik:RadTreeList>
</telerik:RadAjaxPanel>
<h3>Generic list</h3>
<telerik:RadAjaxPanel ID="RadAjaxPanel2" LoadingPanelID="RadAjaxLoadingPanel1" runat="server">
<telerik:RadTreeList RenderMode="Lightweight" runat="server" ID="RadTreeList2" DataKeyNames="ID" ParentDataKeyNames="ParentID"
OnNeedDataSource="RadTreeList2_NeedDataSource" AutoGenerateColumns="false">
<Columns>
<telerik:TreeListBoundColumn DataField="Title" HeaderText="Title" UniqueName="Title">
<HeaderStyle Width="200px" />
</telerik:TreeListBoundColumn>
<telerik:TreeListBoundColumn DataField="Description" HeaderText="Description" UniqueName="Description">
</telerik:TreeListBoundColumn>
</Columns>
</telerik:RadTreeList>
</telerik:RadAjaxPanel>
<h3>DataReader</h3>
<telerik:RadAjaxPanel ID="RadAjaxPanel3" LoadingPanelID="RadAjaxLoadingPanel1" runat="server">
<telerik:RadTreeList RenderMode="Lightweight" runat="server" ID="RadTreeList3" DataKeyNames="EmployeeID" ParentDataKeyNames="ReportsTo"
OnNeedDataSource="RadTreeList3_NeedDataSource" OnDataBound="RadTreeList3_DataBound">
</telerik:RadTreeList>
</telerik:RadAjaxPanel>
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" />
C#
protected void RadTreeList1_NeedDataSource(object sender, TreeListNeedDataSourceEventArgs e)
{
ArrayList list = new ArrayList();
list.Add(new TestListItem(1, "Item 1", 2));
list.Add(new TestListItem(2, "Item 2", null));
list.Add(new TestListItem(3, "Item 3", 2));
list.Add(new TestListItem(4, "Item 4", 5));
list.Add(new TestListItem(5, "Item 5", null));
list.Add(new TestListItem(6, "Item 6", 2));
list.Add(new TestListItem(7, "Item 7", 5));
list.Add(new TestListItem(8, "Item 8", 3));
list.Add(new TestListItem(9, "Item 9", 3));
list.Add(new TestListItem(10, "Item 10", 3));
RadTreeList1.DataSource = list;
}
protected void RadTreeList2_NeedDataSource(object sender, TreeListNeedDataSourceEventArgs e)
{
RadTreeList2.DataSource = MyDataClass.GetData();
}
SqlConnection conn = null;
SqlDataReader reader = null;
protected void RadTreeList3_NeedDataSource(object sender, TreeListNeedDataSourceEventArgs e)
{
String ConnString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
conn = new SqlConnection(ConnString);
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT EmployeeID, LastName, FirstName, Title, TitleOfCourtesy, ReportsTo FROM Employees", conn);
reader = cmd.ExecuteReader();
RadTreeList3.DataSource = reader;
}
protected void RadTreeList3_DataBound(object sender, EventArgs e)
{
reader.Close();
conn.Close();
}
public class MyDataClass
{
public static List<Item> GetData()
{
List<Item> data = new List<Item>();
data.Add(new Item(1, "RadControls for ASP.NET Ajax", "Telerik RadControls for ASP.NET AJAX includes more than 70 controls with proven reliability that help you build high-quality, professional line of business web applications.", 0));
data.Add(new Item(2, "RadControls for Silverlight", "RadControls are built on Microsoft Silverlight and include 40+ UI controls for building rich line-of-business Silverlight applications.", 0));
data.Add(new Item(3, "RadGrid", "Codeless databinding, rich client-side operations, and a myriad of features topped with unbeatable performance is what defines the Telerik RadGrid for ASP.NET AJAX.", 1));
data.Add(new Item(4, "RadEditor", "RadEditor is not simply an HTML Editor. It is what Microsoft chose to use in MSDN, CodePlex, TechNet, MCMS and even as an alternative to the default editor in SharePoint.", 1));
data.Add(new Item(5, "RadScheduler", "With RadScheduler you can add rich scheduling UI to any ASP.NET application and enjoy a lightweight, yet highly customizable, component.", 1));
data.Add(new Item(6, "RadGridView", "RadGridView is the ultimate Silverlight grid control that features unrivalled performance through LINQ-based data engine, remarkably flexible hierarchy model, advanced features such as Excel-like filtering, row details, totals, export to Word/Excel/CSV and many more.", 2));
data.Add(new Item(7, "RadMenu", "Have you confronted the challenge to have to build a very complicated custom site menu system, while still keeping it simple for the end user? With RadMenu for Silverlight you will easily solve this puzzle.", 2));
data.Add(new Item(8, "RadGauge", "If you are building business dashboards or you just need graphical indicators, you will find the new RadGauge control indispensable. ", 2));
return data;
}
}
public class Item
{
public int ID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public int ParentID { get; set; }
public Item(int id, string title, string description, int parentID)
{
ID = id;
Title = title;
Description = description;
ParentID = parentID;
}
}
public class TestListItem
{
public int? ID { get; set; }
public int? PID { get; set; }
public string Text { get; set; }
public TestListItem(int? id, string text, int? pid)
{
ID = id;
Text = text;
PID = pid;
}
}