RadTreeView for ASP.NET AJAX

RadControls for ASP.NET AJAX

Binding RadTreeView to a data source that implements IEnumerable, such as Array or ArrayList, can create a flat data structure as well as a hierarchy if the proper ID -> ParentID relationship is provided.

Here is a general outline of IEnumerable data binding:

  1. Create the collection.

  2. Add values to the collection and set the DataSource property of RadTreeView to the instance of the collection.

  3. Call the DataBind() method.

The collection is automatically mapped to the Text property of the respective TreeView Node. The example below takes an array of strings as a data source.

 
RadTreeView Binding to Array

If you have a collection (any collection implementing ICollection or IEnumerable) that contains objects (as opposed to simple values), you can take advantage of DataTextField, DataNavigateUrlField, and DataValueField properties to map object properties from the object directly to the Text, NavigateUrl or Value fields. If the DataFieldID and DataFieldParentID properties are set, RadTreeView will create a hierarchy of Nodes, determining the Root ones using the following algorithm:

Tip

- their DataFieldParentID property must be null if it is of nullable (e.g. int?) or reference (e.g. string) type.

example:

IDParentID

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:

IDParentID

1 0

2 1

Note

To see hierarchical data binding to IEnumerable in action, please visit the Hierarchical Data Binding live demo.

If you need to map additional properties from the object, subscribe to the NodeDataBound event. The event arguments passed to the event, e.NodeBound and e.NodeBound.DataItem, hold the instance of the TreeView Node being bound and the DataItem associated with the TreeView Node. You can then map a property from the DataItem (make sure to cast the DataItem object to your respective data type first) to a property of the RadTreeNode class.

The example below creates a list of car objects, each with properties for Name, Price and URL. DataTextField is bound to "Name", DataNavigateUrlField is bound to "URL" and DataValueField is bound to "Price". Note that the NodeDataBound event handler sets the Target property of each Node to "_self" so that the URL for each Node will display in a new window. When the Node is clicked, the Text and Value of the Node is displayed in the page title and the Node is selected. Navigation to the URL occurs automatically. Also notice that the NodeDataBound event handler sets the tooltip of the Node based on the Node DataItem. The DataItem is cast to the underlying Car object type.

 
RadTreeView Binding to Array