語言

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() 清單。

範例

以下程式碼範例示範如何在由 PerformDataBinding衍生的類別中實作該DataBoundControl方法。 控制項 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

備註

在從類別衍生出資料綁定控制DataBind項時,應實作這個方法,而不是原本的方法DataBoundControl。 將控制項的資料綁定邏輯放入PerformDataBinding,可以避免DataBindingDataBound事件和事件以錯誤的順序被提出。

雖然基底DataBoundControl類別並未提供此方法的具體實作,但方法會呼叫PerformDataBinding此PerformSelect方法,將任何使用者介面(UI)控制項的值綁定到該方法檢索PerformSelect的資料上。

適用於