This documentation is archived and is not being maintained.

HierarchicalDataBoundControl Class

Serves as the base class for all ASP.NET version 2.0 data-bound controls that display their data in hierarchical form.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public abstract class HierarchicalDataBoundControl : BaseDataBoundControl
<asp:HierarchicalDataBoundControl />

The HierarchicalDataBoundControl class is the base class used for ASP.NET controls that retrieve data from an ASP.NET hierarchical data source control and bind user-interface elements of the control to that data for display. The TreeView and Menu classes derive from HierarchicalDataBoundControl.

Page developers do not use the HierarchicalDataBoundControl class directly; instead, they use controls that derive from this class.

Controls developers extend this class to create data-bound controls that work with classes that implement the IHierarchicalDataSource interface and classes that derive from the HierarchicalDataSourceControl and HierarchicalDataSourceView classes. When deriving a class from the HierarchicalDataBoundControl class, override the PerformDataBinding method to bind the user-interface elements of your control to data retrieved by the GetData method. In most cases, the PerformDataBinding method is the only method you will override in your derived class.

For ASP.NET 2.0 data-bound controls, the PerformSelect method is the equivalent of the DataBind method, and is called to bind data at run time. The PerformSelect method calls the GetData and PerformDataBinding methods.

The following code example demonstrates how to derive a class from the HierarchicalDataBoundControl class to create a custom data-bound control. The GeneologyTree control renders a pre HTML section with a text tree for the data retrieved from an associated data source control.

using System;
using System.Collections;
using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Samples.AspNet.CS.Controls {

    public class GeneologyTree : HierarchicalDataBoundControl {

        private TreeNode rootNode;
        public TreeNode RootNode {
            get {
                if (rootNode == null) {
                    rootNode = new TreeNode(String.Empty);
                return rootNode;

        private ArrayList nodes;
        public ArrayList Nodes {
            get {
                if (null == nodes) {
                    nodes = new ArrayList();
                return nodes;
        public string DataTextField {
            get {
                object o = ViewState["DataTextField"];
                return((o == null) ? string.Empty : (string)o);
            set {
                ViewState["DataTextField"] = value;
                if (Initialized) {
        private int _maxDepth = 0;
        protected override void PerformDataBinding() {

            // Do not attempt to bind data if there is no 
            // data source set. 
            if (!IsBoundUsingDataSourceID && (DataSource == null)) {

            HierarchicalDataSourceView view = GetData(RootNode.DataPath);

            if (view == null) {
                throw new InvalidOperationException
                    ("No view returned by data source control.");

            IHierarchicalEnumerable enumerable = view.Select();
            if (enumerable != null) {


                try {
                    RecurseDataBindInternal(RootNode, enumerable, 1);
                finally {

        private void RecurseDataBindInternal(TreeNode node, 
            IHierarchicalEnumerable enumerable, int depth) {                                    

            foreach(object item in enumerable) {
                IHierarchyData data = enumerable.GetHierarchyData(item);

                if (null != data) {
                    // Create an object that represents the bound data 
                    // to the control.
                    TreeNode newNode = new TreeNode();
                    RootViewNode rvnode = new RootViewNode();

                    rvnode.Node = newNode;
                    rvnode.Depth = depth;

                    // The dataItem is not just a string, but potentially 
                    // an XML node or some other container.  
                    // If DataTextField is set, use it to determine which  
                    // field to render. Otherwise, use the first field.                     
                    if (DataTextField.Length > 0) {
                        newNode.Text = DataBinder.GetPropertyValue
                            (data, DataTextField, null);
                    else {
                        PropertyDescriptorCollection props = 

                        // Set the "default" value of the node.
                        newNode.Text = String.Empty;                        

                        // Set the true data-bound value of the TextBox, 
                        // if possible. 
                        if (props.Count >= 1) {                        
                            if (null != props[0].GetValue(data)) {
                                newNode.Text = 


                    if (data.HasChildren) {
                        IHierarchicalEnumerable newEnumerable = 
                        if (newEnumerable != null) {                            
                                newEnumerable, depth+1 );

                    if ( _maxDepth < depth) _maxDepth = depth;

        protected override void Render(HtmlTextWriter writer) {

            int currentDepth = 1;
            int currentTextLen = 0;

            foreach (RootViewNode rvnode in Nodes) {
                if (rvnode.Depth == currentDepth) {
                    string output = "  " + rvnode.Node.Text + "  ";
                    currentTextLen = currentTextLen + output.Length;
                else {
                    // Some very basic whitespace formatting 
                    int halfLine = currentTextLen / 2;
                    for (int i=0;i<halfLine;i++) {
                        writer.Write(' ');
                    currentTextLen = 0;
                    for (int j=0;j<halfLine;j++) {
                        writer.Write(' ');
                    string output = "  " + rvnode.Node.Text + "  ";
                    currentTextLen = currentTextLen + output.Length;                    

        private class RootViewNode { 
            public TreeNode Node;
            public int Depth;

The following code example demonstrates how to use the GeneologyTree control, defined in the previous example, and bind it to an XmlDataSource control.

<%@Page language="c#" %>
<%@ Register TagPrefix="aspSample" 
    Assembly="Samples.AspNet.CS.Controls" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<html xmlns="" >
    <title>C# Example</title>

    <form id="Form1" method="post" runat="server">

          datasourceid="XmlDataSource1" />

          runat="server" />


The geneology.xml file accessed in the code example contains the following data.

  <member title="great-grandfather">
    <member title="grandfather" >
      <member title="child" />
      <member title="father" >
         <member title="son" />


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0