Export (0) Print
Expand All

HierarchicalDataBoundControl.PerformDataBinding Method

When overridden in a derived class, binds data from the data source to the control.

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

'Declaration
Protected Friend Overridable Sub PerformDataBinding

Implement this method instead of the DataBind method when you derive a data-bound control from the HierarchicalDataBoundControl class. Placing your control's data-binding logic in PerformDataBinding keeps the DataBinding and DataBound events from being raised in the wrong order.

While the base HierarchicalDataBoundControl class provides no specific implementation for this method, the PerformDataBinding method is called by the PerformSelect method to bind the values of any user interface controls to the data that is retrieved by the PerformSelect method.

The following code example demonstrates how to implement the PerformDataBinding method in a class derived from HierarchicalDataBoundControl. The GeneologyTree control iterates through the IHierarchicalEnumerable and IHierarchyData objects retrieved from its associated HierarchicalDataSourceView, and creates a text tree structure for the data it is bound to. This code example is part of a larger example provided for the HierarchicalDataBoundControl class.

        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 ' 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)
                    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 'RecurseDataBindInternal

.NET Framework

Supported in: 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