DataBoundControl.PerformDataBinding(IEnumerable) Método

Definição

Quando substituído em uma classe derivada, associa dados da fonte de dados ao controle.

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)

Parâmetros

data
IEnumerable

A lista de dados IEnumerable retornada de uma chamada de método PerformSelect().

Exemplos

O exemplo de código a seguir demonstra como implementar o PerformDataBinding método em uma classe derivada de DataBoundControl. O TextBoxSet controle cria um TextBox controle para cada item de dados ao qual está associado. Este exemplo de código faz parte de um exemplo maior fornecido para a DataBoundControl classe .

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

Comentários

Implemente esse método em vez do DataBind método quando você derivar um controle associado a dados da DataBoundControl classe . Colocar a lógica de associação de dados do controle no PerformDataBinding permite evitar que os DataBinding eventos e DataBound sejam gerados na ordem errada.

Embora a classe base DataBoundControl não forneça nenhuma implementação específica para esse método, o PerformDataBinding método é chamado pelo PerformSelect método para associar os valores de qualquer controle de interface do usuário aos dados recuperados pelo PerformSelect método .

Aplica-se a