HierarchicalDataBoundControl.GetData Method (String)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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 Overridable Function GetData (
	viewPath As String
) As HierarchicalDataSourceView


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.

Exception Condition

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 Overrides Sub PerformDataBinding()

    ' Do not attempt to bind data if there is no
    ' data source set.
    If Not IsBoundUsingDataSourceID AndAlso DataSource Is Nothing Then
    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


            RecurseDataBindInternal(RootNode, enumerable, 1)
        End Try
    End If

End Sub ' PerformDataBinding

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)
                Dim props As PropertyDescriptorCollection = _

                ' 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


            If data.HasChildren Then
                Dim newEnumerable As IHierarchicalEnumerable = _
                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 'RecurseDataBindInternal

.NET Framework
Available since 2.0
Return to top