共用方式為


HierarchicalDataBoundControl.PerformDataBinding 方法

定義

在衍生類別中覆寫時,會將資料從資料來源繫結至控制項。

protected public:
 virtual void PerformDataBinding();
protected internal virtual void PerformDataBinding ();
abstract member PerformDataBinding : unit -> unit
override this.PerformDataBinding : unit -> unit
Protected Friend Overridable Sub PerformDataBinding ()

範例

下列程式碼範例示範如何在衍生自 HierarchicalDataBoundControl 的類別中實 PerformDataBinding 作 方法。 控制項 GeneologyTree 會逐 IHierarchicalEnumerable 一查看從其相關聯 HierarchicalDataSourceView 擷取的 和 IHierarchyData 物件,並為其系結的資料建立文字樹狀結構。 此程式碼範例是提供給 類別之較大範例的 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

備註

當您從 HierarchicalDataBoundControl 類別衍生資料繫結控制項時, DataBind 請實作這個方法,而不是 方法。 將控制項的資料系結邏輯放在 中 PerformDataBinding ,可讓 DataBindingDataBound 事件以錯誤的順序引發。

雖然基 HierarchicalDataBoundControl 類未提供這個方法的特定實作, PerformDataBinding 但 方法會由 PerformSelect 方法呼叫,以將任何使用者介面控制項的值系結至 方法所 PerformSelect 擷取的資料。

適用於