Accessing Cells and Items
There are various ways to access the different items of the RadTreeList control.The most important ones are shown below.
Accessing TreeListDataItems and their values
You can access the RadTreeList data items either using the item events of the control(ItemCreated, ItemDataBound, ItemCommand) or by looping through its Items collection when it is available.
//e is the event argument object
if (e.Item is TreeListDataItem)
{
TreeListDataItem item = e.Item as TreeListDataItem;
}
foreach (TreeListDataItem item in RadTreeList1.Items)
{
//perform some action with the data item
}
Once you get hold of a certain data item, you can access its cells by using the UniqueName property of the column that the cell belongs to.
TableCell cell = dataItem["ColumnUniqueName"]; //where dataItem is an object of type TreeListDataItem
With bound columns you can use the Textproperty to get the value of the cell.
string itemValue = cell.Text;
For values in TreeListTemplateColumns you will need to find the control in the treelist cell (either using the FindControl() method or the Controls collection of the cell) and extract its value.
string title = (dataItem["ColumnUniqueName"].FindControl("Label1") as Label).Text;
In case you want to access a checkbox in a TreeListCheckBoxColumn, you will be able to get hold of it through the cell's Controls collection.
CheckBox chk = dataItem["Qualifies"].Controls[0] as CheckBox;
If you need to get hold of the expand/collapse button in the treelist data item you can do so by using FindControl() having in mind that the button id is "ExpandCollapseButton":
if (dataItem.FindControl("ExpandCollapseButton") != null)
{
Button btn = dataItem.FindControl("ExpandCollapseButton") as Button;
}
Accessing the TreeListHeaderItem
You can use the ItemCreated and ItemDataBound events Of RadTreeListto get hold of the header.
if (e.Item is TreeListHeaderItem)
{
TreeListHeaderItem header = e.Item as TreeListHeaderItem;
TableCell headerCell = header["ColumnUniqueName"] as TableCell;
}
Same as with the TreeListDataItem, you can access the separate cells by using the UniqueName of the column in question.
TableCell headerCell = header["ColumnUniqueName"] as TableCell;
Accessing the TreeListPagerItem
You can use the same approach to get hold of the pager as for the header:
if (e.Item is TreeListPagerItem)
{
TreeListPagerItem pager = e.Item as TreeListPagerItem;
pager.PagerContentCell.BackColor = System.Drawing.Color.AliceBlue;
}
Accessing the TreeListDetailTemplateItem
The ItemCreated and ItemDataBound events are the place where you can access the TreeListDetailTemplateItem as well.
if (e.Item is TreeListDetailTemplateItem)
{
TreeListDetailTemplateItem detailItem = e.Item as TreeListDetailTemplateItem;
Label lbl = detailItem.FindControl("Label1") as Label;
}
After that you can use the FindControl() method to get reference to the controls specified in the template.
Label lbl = detailItem.FindControl("Label1") as Label;
Accessing the TreeListNoRecordsItem
You can access the item rendered when the treelist is bound to an empty data source again the same way:
if (e.Item is TreeListNoRecordsItem)
{
TreeListNoRecordsItem noRecordsItem = e.Item as TreeListNoRecordsItem;
}
Finding a TreeListDataItem by Key Value
RadTreeList provides a method to search for an existing item via the data key values of the underlying record. This method supports both key and parent key parameters.
protected void Button1_Click(object sender, EventArgs e)
{
TreeListDataItem item = RadTreeList1.FindItemByKeyValue("RecordID", 3);
}
If no items are found, the method returns null. In case there are multiple records matching the parameters, the method will return only the first item.