Export (0) Print
Expand All

IHierarchyData.HasChildren Property

Indicates whether the hierarchical data node that the IHierarchyData object represents has any child nodes.

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

bool HasChildren { get; }

Property Value

Type: System.Boolean
true if the current node has child nodes; otherwise, false.

The HasChildren property is a convenience property that enables callers to avoid checking the return value of the GetChildren method for null. If the HasChildren property returns true, call the GetChildren method to retrieve an IHierarchicalEnumerable collection of child nodes.

The following code example demonstrates how an ASP.NET hierarchical data-bound control uses an IHierarchyData object in a recursive data-binding method. The items in an IHierarchicalEnumerable collection are enumerated, and for each an IHierarchyData object is retrieved using the GetHierarchyData method. Finally, the HasChildren property is checked to determine whether to recurse. This code example is part of a larger example provided for the HierarchicalDataBoundControl class.

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;

        }
    }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft