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, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft