ASP.net(C#) Custom Hierarchical Datasource

April 11, 2010 by C#   ASP.NET  

Some time ago I wrote a post on populating a treeview control within windows forms. The same method (slightly altered) can be used to populate a treeview control in asp.net, even though its not quite what microsoft put in place with regards to binding hierarchical data.

One of these mechanisms (that they put in place) are the XmlDataSource control - which we can use to bind hierarchical data to a treeview control, lets have a quick look.

The xml we're going to use will look something like this:

<?xml version="1.0" encoding="utf-8" ?>
<nodes title="Programming Languages">
  <node title="Microsoft">
    <node title="C#">
      <node title="LinQ"></node>
    </node>
    <node title="VB.net"></node>
  </node>
  <node title="Open Source">
    <node title="Python"></node>
    <node title="Ruby"></node>
    <node title="PHP">
      <node title="5.2"></node>
      <node title="4.4"></node>
    </node>
    <node title="Perl"></node>
    <node title="Java"></node>
  </node>
</nodes>

The ASP.net markup will look something like this:
<asp:TreeView ID="tree" runat="server" DataSourceID="source" ExpandDepth="0">
    <DataBindings>
        <asp:TreeNodeBinding DataMember="node" TextField="title" />
        <asp:TreeNodeBinding DataMember="nodes" TextField="title" />
    </DataBindings>
</asp:TreeView>
<asp:XmlDataSource ID="source" runat="server" DataFile="~/xml/programming.xml">
</asp:XmlDataSource>

Obviously (looking at our TreeNodeBinding node) DataMember refers to the xml tag e.g. node & nodes, TextField refers to the attributes within the node.

This is basically a flat file approach, but what if we need to bind using SQL for example? We can always serialize our dataset/table to xml and bind it to an XmlDataSource - or alternatively one can bind using a quick and dirty method like this.

But lets make it more interesting and create our own hierarchical source, we can achieve this by using the IHierarchyData, IHierarchicalEnumerable interfaces and HierarchicalDataSourceView, HierarchicalDataSourceControl classes.

However instead of using the HierarchicalDataSourceControl class, I am going to inherit from the ObjectDataSource class and create a HierarchicalObjectDataSource:
public class HierarchicalObjectDataSource : ObjectDataSource, IHierarchicalDataSource
{
    public event EventHandler DataSourceChanged;

    public String DataTextField
    {
        get;
        set;
    }

    public String DataValueField
    {
        get;
        set;
    }

    public String DataParentField
    {
        get;
        set;
    }

    public HierarchicalDataSourceView GetHierarchicalView(string viewPath)
    {
        return new HierarchicalObjectDataSourceView(this, viewPath);
    }
}

The hierarchical data item will look something like this:
public class Hierarchical : IHierarchyData
{
    public HierarchicalCollection source
    {
        get;
        set;
    }

    public Int32 Value
    {
        get;
        set;
    }

    public Int32 ParentID
    {
        get;
        set;
    }

    public String Text
    {
        get;
        set;
    }

    private Hierarchical()
    {
    }

    public Hierarchical(int value, int parentID, string text)
    {
        this.Value = value;
        this.ParentID = parentID;
        this.Text = text;
    }

    public IHierarchicalEnumerable GetChildren()
    {
        HierarchicalCollection children = new HierarchicalCollection();

        foreach (Hierarchical Hierarchical in this.source)
        {
            if (Hierarchical.ParentID == this.Value)
            {
                children.Add(Hierarchical);
            }
        }
        return children;
    }

    public IHierarchyData GetParent()
    {
        foreach (Hierarchical Hierarchical in this.source)
        {
            if (Hierarchical.Value == this.ParentID)
                return Hierarchical;
        }
        return null;
    }

    public bool HasChildren
    {
        get
        {
            HierarchicalCollection children = GetChildren() as HierarchicalCollection;
            return children.Count > 0;
        }
    }

    public object Item
    {
        get { return this; }
    }

    public string Path
    {
        get { return this.Value.ToString(); }
    }

    public string Type
    {
        get { return this.GetType().ToString(); }
    }

    public override string ToString()
    {
        return this.Text;
    }
}

The collection used for binding will look like this:
public class HierarchicalCollection : List<Hierarchical>, IHierarchicalEnumerable
{
    public HierarchicalCollection()
        : base()
    {
    }

    public IHierarchyData GetHierarchyData(object enumeratedItem)
    {
        return enumeratedItem as IHierarchyData;
    }
}

The class used for collecting data for the HierarchicalObjectDataSource:
public class HierarchicalObjectDataSourceView : HierarchicalDataSourceView
{
    private string _viewPath;
    private HierarchicalCollection _source = new HierarchicalCollection();

    public HierarchicalObjectDataSourceView(HierarchicalObjectDataSource source, string viewPath)
    {
        _viewPath = viewPath;
        CreateCollection(source);
    }

    private void CreateCollection(HierarchicalObjectDataSource source)
    {
        IEnumerable data = source.Select();
        if (data != null)
        {
            foreach (Object dataItem in data)
            {
                Int32 value = Convert.ToInt32(DataBinder.GetPropertyValue(dataItem, source.DataValueField, null));
                Int32 parentID = Convert.ToInt32(DataBinder.GetPropertyValue(dataItem, source.DataParentField, null));
                String text = DataBinder.GetPropertyValue(dataItem, source.DataTextField, null);
                _source.Add(new Hierarchical(value, parentID, text));
            }
        }
    }

    public override IHierarchicalEnumerable Select()
    {
        HierarchicalCollection collection = new HierarchicalCollection();
        Int32 ParentId = (!String.IsNullOrEmpty(_viewPath)) ? Convert.ToInt32(_viewPath) : 0;

        foreach (Hierarchical Hierarchical in this._source)
        {
            if (Hierarchical.ParentID == ParentId)
            {
                Hierarchical.source = _source;
                collection.Add(Hierarchical);
            }
        }
        return collection;
    }
}

Eventually we simply bind our object (which is SQL/XML whatever based) like this:
<asp:TreeView ID="tree" runat="server" DataSourceID="source" ExpandDepth="0">
</asp:TreeView>
<asp:HierarchicalObjectDataSource ID="source" runat="server" SelectMethod="GetData"
	TypeName="CSTruter.com.Data" DataParentField="ParentID" DataValueField="Value"
	DataTextField="Text" />


Leave a Comment


Developer February 29, 2012 by Craig

Hi Mr. Truter, This is fantastic code. I have implemented a version of this in my own project and it all works nicely. I am currently trying to solve one issue: I receive an (int)ID back from the database which represents the ID of a node somewhere in my tree (could be any depth level) 1. How do I find the node in the tree which matches my ID (I'm trying to use LINQ: TreeNode selectedNode = this.myTreeOnPage.Nodes.OfType<TreeNode>().FirstOrDefault(node => node.DataPath == Convert.ToString(TreeIDFromDB)) but am also considering a good old foreach loop instead. 2. How do I expand only the parent nodes in the path to the selected node? Any suggestions you can provide is greatly appreciated! Thanks, Craig

October 7, 2010 by Christoff Truter

Ah k, I understand - when/if I get some time this week, I will improve the example a bit. Thanks for pointing this out

Value doesn't return what I set it too October 6, 2010 by Anonymous

I was able to get the Value back in the DataPath like you said. I was just expecting it to behave the same way as when I programmatically add a TreeNode and set the Value as the key value and it returns in the .Value property. In the demo I get back the Text in the Value field and the key in the DataPath.

October 6, 2010 by Christoff Truter

Hi there Using the demo code one can extract all selected data via SelectedNode, DataPath (the actual key e.g. 2 if you select microsoft) ValuePath (Like a breadcrumb) Text - (The display value) via selectednodechanged event? Or am I misunderstanding the problem?

It returns the same value for Value, Path, Text October 5, 2010 by Anonymous

I was trying to use this but discovered that the Value return is the same as the Text and Path. It doesn't return the Value back.


    Related Posts

    Populate a TreeView Control C#

    August 27, 2009


    Related Downloads

    ASP.NET webforms custom hierarchical datasource demo