HierarchicalDataBoundControl Class
Assembly: System.Web (in system.web.dll)
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 { [AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.Minimal)] [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)] 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) { OnDataPropertyChanged(); } } } private int _maxDepth = 0; protected override void PerformDataBinding() { base.PerformDataBinding(); // Do not attempt to bind data if there is no // data source set. if (!IsBoundUsingDataSourceID && (DataSource == null)) { return; } 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) { Nodes.Clear(); 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 = 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) { if (null != props[0].GetValue(data)) { newNode.Text = props[0].GetValue(data).ToString(); } } } Nodes.Add(rvnode); if (data.HasChildren) { IHierarchicalEnumerable newEnumerable = data.GetChildren(); if (newEnumerable != null) { RecurseDataBindInternal(newNode, newEnumerable, depth+1 ); } } if ( _maxDepth < depth) _maxDepth = depth; } } } protected override void Render(HtmlTextWriter writer) { writer.WriteLine("<PRE>"); int currentDepth = 1; int currentTextLen = 0; foreach (RootViewNode rvnode in Nodes) { if (rvnode.Depth == currentDepth) { string output = " " + rvnode.Node.Text + " "; writer.Write(output); currentTextLen = currentTextLen + output.Length; } else { writer.WriteLine(""); // Some very basic whitespace formatting int halfLine = currentTextLen / 2; for (int i=0;i<halfLine;i++) { writer.Write(' '); } writer.Write('|'); writer.WriteLine(""); ++currentDepth; currentTextLen = 0; for (int j=0;j<halfLine;j++) { writer.Write(' '); } string output = " " + rvnode.Node.Text + " "; writer.Write(output); currentTextLen = currentTextLen + output.Length; } } writer.WriteLine("</PRE>"); } 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" Namespace="Samples.AspNet.CS.Controls" Assembly="Samples.AspNet.CS.Controls" %> <html> <head> <title>C# 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>
- AspNetHostingPermission for operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.
- AspNetHostingPermission for operating in a hosted environment. Demand value: InheritanceDemand; Permission value: Minimal.
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
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.