DataBoundControl.PerformDataBinding(IEnumerable) 方法

定义

在派生类中重写时,将数据源中的数据绑定到控件。

protected public:
 virtual void PerformDataBinding(System::Collections::IEnumerable ^ data);
protected internal virtual void PerformDataBinding (System.Collections.IEnumerable data);
abstract member PerformDataBinding : System.Collections.IEnumerable -> unit
override this.PerformDataBinding : System.Collections.IEnumerable -> unit
Protected Friend Overridable Sub PerformDataBinding (data As IEnumerable)

参数

data
IEnumerable

IEnumerable 方法调用返回的数据的 PerformSelect() 列表。

示例

下面的代码示例演示如何在派生自 DataBoundControl的类中实现 PerformDataBinding 方法。 控件 TextBoxSet 为其绑定到的每个数据项创建一个 TextBox 控件。 此代码示例是为 DataBoundControl 类提供的一个更大示例的一部分。

protected override void PerformDataBinding(IEnumerable retrievedData) {
    base.PerformDataBinding(retrievedData);

    // If the data is retrieved from an IDataSource as an 
    // IEnumerable collection, attempt to bind its values to a 
    // set of TextBox controls.
    if (retrievedData != null) {

        foreach (object dataItem in retrievedData) {
            
            TextBox box = new TextBox();
            
            // The dataItem is not just a string, but potentially
            // a System.Data.DataRowView 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) {
                box.Text = DataBinder.GetPropertyValue(dataItem, 
                    DataTextField, null);
            }
            else {
                PropertyDescriptorCollection props = 
                    TypeDescriptor.GetProperties(dataItem);

                // Set the "default" value of the TextBox.
                box.Text = String.Empty;
                
                // Set the true data-bound value of the TextBox,
                // if possible.
                if (props.Count >= 1) {                        
                    if (null != props[0].GetValue(dataItem)) {
                        box.Text = props[0].GetValue(dataItem).ToString();
                    }
                }
            }                                        
            
            BoxSet.Add(box);
        }
    }
}
Protected Overrides Sub PerformDataBinding(ByVal retrievedData As IEnumerable)
    MyBase.PerformDataBinding(retrievedData)

    ' If the data is retrieved from an IDataSource as an IEnumerable 
    ' collection, attempt to bind its values to a set of TextBox controls.
    If Not (retrievedData Is Nothing) Then

        Dim dataItem As Object
        For Each dataItem In retrievedData

            Dim box As New TextBox()

            ' The dataItem is not just a string, but potentially
            ' a System.Data.DataRowView 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
                box.Text = DataBinder.GetPropertyValue( _
                dataItem, DataTextField, Nothing)
            Else
                Dim props As PropertyDescriptorCollection = _
                    TypeDescriptor.GetProperties(dataItem)

                ' Set the "default" value of the TextBox.
                box.Text = String.Empty

                ' Set the true data-bound value of the TextBox,
                ' if possible.
                If props.Count >= 1 Then
                    If props(0).GetValue(dataItem) IsNot Nothing Then
                        box.Text = props(0).GetValue(dataItem).ToString()
                    End If
                End If
            End If

            BoxSet.Add(box)
        Next dataItem
    End If

End Sub

注解

DataBoundControl 类派生数据绑定控件时,DataBind实现此方法而不是 方法。 将 控件的数据绑定逻辑置于 中 PerformDataBinding 可以避免 DataBinding 以错误的顺序引发 和 DataBound 事件。

虽然基 DataBoundControl 类没有为此方法提供特定的实现, PerformDataBinding 但 方法会 PerformSelect 调用 方法,以将任何用户界面 (UI) 控件的值绑定到方法检索 PerformSelect 的数据。

适用于