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)

'Declaration
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public MustInherit Class HierarchicalDataBoundControl _
	Inherits BaseDataBoundControl
'Usage
Dim instance As HierarchicalDataBoundControl
<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.

Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Security.Permissions
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace Samples.AspNet.VB.Controls

    <AspNetHostingPermission(SecurityAction.Demand, _
      Level:=AspNetHostingPermissionLevel.Minimal), _
      AspNetHostingPermission(SecurityAction.InheritanceDemand, _
      Level:=AspNetHostingPermissionLevel.Minimal)> _
    Public Class GeneologyTree
        Inherits HierarchicalDataBoundControl

        Dim MaxDepth As Integer = 0

        Private aRootNode As TreeNode
        Public ReadOnly Property RootNode() As TreeNode
            Get 
                If aRootNode Is Nothing Then
                    aRootNode = New TreeNode(String.Empty)
                End If 
                Return aRootNode
            End Get 
        End Property 

        Private alNodes As ArrayList
        Public ReadOnly Property Nodes() As ArrayList
            Get 
                If alNodes Is Nothing Then
                    alNodes = New ArrayList()
                End If 
                Return alNodes
            End Get 
        End Property 

        Public Property DataTextField() As String 
            Get 
                Dim o As Object = ViewState("DataTextField")
                If o Is Nothing Then 
                    Return String.Empty
                Else 
                    Return CStr(o)
                End If 
            End Get 
            Set(ByVal value As String)
                ViewState("DataTextField") = value
                If Initialized Then
                    OnDataPropertyChanged()
                End If 
            End Set 
        End Property 

        Protected Overrides Sub PerformDataBinding()
            MyBase.PerformDataBinding()

            ' Do not attempt to bind data if there is no 
            ' data source set. 
            If Not IsBoundUsingDataSourceID AndAlso DataSource Is Nothing Then 
                Return 
            End If 

            Dim view As HierarchicalDataSourceView = GetData(RootNode.DataPath)

            If view Is Nothing Then 
                Throw New InvalidOperationException _
                ("No view returned by data source control.")
            End If 

            Dim enumerable As IHierarchicalEnumerable = view.Select()
            If Not (enumerable Is Nothing) Then

                Nodes.Clear()

                Try
                    RecurseDataBindInternal(RootNode, enumerable, 1)
                Finally 
                End Try 
            End If 

        End Sub ' PerformDataBinding

        Private Sub RecurseDataBindInternal(ByVal node As TreeNode, _
            ByVal enumerable As IHierarchicalEnumerable, _
            ByVal depth As Integer)

            Dim item As Object
            For Each item In enumerable

                Dim data As IHierarchyData = enumerable.GetHierarchyData(item)

                If Not data Is Nothing Then 

                    ' Create an object that represents the bound data 
                    ' to the control. 
                    Dim newNode As New TreeNode()
                    Dim rvnode As 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 Then
                        newNode.Text = DataBinder.GetPropertyValue _
                        (data, DataTextField, Nothing)
                    Else 
                        Dim props As PropertyDescriptorCollection = _
                        TypeDescriptor.GetProperties(data)

                        ' 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 Then 
                            If Not props(0).GetValue(data) Is Nothing Then
                                newNode.Text = props(0).GetValue(data).ToString()
                            End If 
                        End If 
                    End If

                    Nodes.Add(rvnode)

                    If data.HasChildren Then 
                        Dim newEnumerable As IHierarchicalEnumerable = _
                            data.GetChildren()
                        If Not (newEnumerable Is Nothing) Then
                            RecurseDataBindInternal(newNode, _
                            newEnumerable, depth + 1)
                        End If 
                    End If 

                    If MaxDepth < depth Then
                        MaxDepth = depth
                    End If 
                End If 
            Next item

        End Sub 'RecurseDataBindInternal

        Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)

            writer.WriteLine("<PRE>")
            Dim currentDepth As Integer = 1
            Dim currentTextLen As Integer = 0

            Dim rvnode As RootViewNode
            For Each rvnode In Nodes
                If rvnode.Depth = currentDepth Then 
                    Dim output As String = "  " + rvnode.Node.Text + "  "
                    writer.Write(output)
                    currentTextLen = currentTextLen + output.Length
                Else
                    writer.WriteLine("")

                    ' Some very basic whitespace formatting. 
                    ' The implicit conversion to an Integer is fine, as  
                    ' a general estimate is acceptable for this  
                    ' simple example. 
                    Dim halfLine As Integer = CInt(currentTextLen / 2)
                    Dim i As Integer 
                    For i = 0 To halfLine
                        writer.Write(" "c)
                    Next i
                    writer.Write("|"c)
                    writer.WriteLine("")
                    currentDepth += 1
                    Dim j As Integer 
                    For j = 0 To halfLine
                        writer.Write(" "c)
                    Next j
                    Dim output As String = "  " + rvnode.Node.Text + "  "
                    writer.Write(output)
                    currentTextLen = currentTextLen + output.Length
                End If 
            Next rvnode
            writer.WriteLine("</PRE>")

        End Sub 'Render


        Private Class RootViewNode
            Public Node As TreeNode
            Public Depth As Integer 
        End Class 'RootViewNode
    End Class 'GeneologyTree
End Namespace

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="VB" %>
<%@ Register TagPrefix="aspSample"  
    Namespace="Samples.AspNet.VB.Controls" 
    Assembly="Samples.AspNet.VB.Controls" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>VB Example</title>
  </head>

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

        <aspSample:geneologytree
          id="GeneologyTree1"
          runat="server"
          datatextfield="title"
          datasourceid="XmlDataSource1" />

        <asp:xmldatasource
          id="XmlDataSource1"
          datafile="geneology.xml"          
          runat="server" />

    </form>
  </body>
</html>

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

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

System.Object
  System.Web.UI.Control
    System.Web.UI.WebControls.WebControl
      System.Web.UI.WebControls.BaseDataBoundControl
        System.Web.UI.WebControls.HierarchicalDataBoundControl
          System.Web.UI.WebControls.Menu
          System.Web.UI.WebControls.TreeView

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
Show: