Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

HierarchicalDataBoundControl.GetData Method

Retrieves a HierarchicalDataSourceView object that the data-bound control uses to perform data operations.

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

protected virtual HierarchicalDataSourceView GetData(
	string viewPath
)

Parameters

viewPath
Type: System.String

The hierarchical path of the view to retrieve.

Return Value

Type: System.Web.UI.HierarchicalDataSourceView
The HierarchicalDataSourceView object that the data-bound control uses to perform data operations.

ExceptionCondition
InvalidOperationException

A HierarchicalDataSourceView could not be retrieved for the specified viewPath.

The GetData method retrieves a HierarchicalDataSourceView object from the associated data source control by calling the GetDataSource method.

The following code example demonstrates how the GetData method is called to retrieve the HierarchicalDataSourceView object from the associated data source control and how the HierarchicalDataSourceView.Select method is called to retrieve the data. This code example is part of a larger example provided for the HierarchicalDataBoundControl class.

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;

        }
    }
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0
Show:
© 2015 Microsoft