HierarchicalDataBoundControl.GetData(String) 方法

定义

检索数据绑定控件用于执行数据操作的 HierarchicalDataSourceView 对象。

protected:
 virtual System::Web::UI::HierarchicalDataSourceView ^ GetData(System::String ^ viewPath);
protected virtual System.Web.UI.HierarchicalDataSourceView GetData (string viewPath);
abstract member GetData : string -> System.Web.UI.HierarchicalDataSourceView
override this.GetData : string -> System.Web.UI.HierarchicalDataSourceView
Protected Overridable Function GetData (viewPath As String) As HierarchicalDataSourceView

参数

viewPath
String

要检索的视图的分层路径。

返回

HierarchicalDataSourceView 对象,数据绑定控件使用此对象执行数据操作。

例外

对于指定的 viewPath,未能检索到 HierarchicalDataSourceView

示例

下面的代码示例演示了如何 GetData 调用 方法以从关联的数据源控件检索 HierarchicalDataSourceView 对象,以及如何 HierarchicalDataSourceView.Select 调用 方法检索数据。 此代码示例是为 HierarchicalDataBoundControl 类提供的一个更大示例的一部分。

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

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

注解

方法GetData通过调用 GetDataSource 方法从关联的数据源控件中检索 HierarchicalDataSourceView 对象。

适用于

另请参阅