DataBoundControl Class

Serves as the base class for all ASP.NET version 2.0 data-bound controls that display their data in list or tabular form.

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

Public MustInherit Class DataBoundControl
	Inherits BaseDataBoundControl
Dim instance As DataBoundControl

public abstract class DataBoundControl extends BaseDataBoundControl
public abstract class DataBoundControl extends BaseDataBoundControl
Not applicable.

The DataBoundControl class is the base class used for ASP.NET controls that retrieve tabular or list-style data from an ASP.NET data source control and bind user-interface (UI) elements of the control to that data for display. Composite data-bound controls such as GridView, DetailsView, and FormView; list-style data-bound controls such as BulletedList and CheckBoxList; and other controls such as AdRotator derive from DataBoundControl.

Page developers do not use the DataBoundControl class directly; instead, they use controls that derive from this class.

Control developers extend this class to create data-bound controls that work with classes that implement the IDataSource interface and classes that derive from the DataSourceControl and DataSourceView classes. When deriving a class from the DataBoundControl class, override the PerformDataBinding method to bind the UI elements of your control to data retrieved by the GetData method. In most cases, the PerformDataBinding method is the only method you will override in your derived class.

For ASP.NET 2.0 data-bound controls, the PerformSelect method is the equivalent of the DataBind method, and is called to bind data at run time. The PerformSelect method calls the GetData and PerformDataBinding methods.

The following code example demonstrates how to derive a class from the DataBoundControl class to create a custom data-bound control. The TextBoxSet control creates a TextBox control for each data item retrieved from its associated data source control, and binds to the value of the data item at run time. The current implementation of the Render method renders the TextBox controls as an unordered list.

Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Security.Permissions
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace Samples.AspNet.Controls.VB

<AspNetHostingPermission(SecurityAction.Demand, _
    Level:=AspNetHostingPermissionLevel.Minimal), _
    AspNetHostingPermission(SecurityAction.InheritanceDemand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  Public Class TextBoxSet
    Inherits DataBoundControl

    Private alBoxSet As IList

    Public ReadOnly Property BoxSet() As IList
            If alBoxSet Is Nothing Then
                alBoxSet = New ArrayList()
            End If
            Return alBoxSet
        End Get
    End Property

    Public Property DataTextField() As String
            Dim o As Object = ViewState("DataTextField")
            If o Is Nothing Then
                Return String.Empty
                Return CStr(o)
            End If
        End Get
        Set(ByVal value As String)
            ViewState("DataTextField") = value
            If (Initialized) Then
            End If
        End Set
    End Property

    Protected Overrides Sub PerformSelect()

        ' Call OnDataBinding here if bound to a data source using the 
        ' DataSource property (instead of a DataSourceID) because the 
        ' data-binding statement is evaluated before the call to GetData.
        If Not IsBoundUsingDataSourceID Then
        End If

        ' The GetData method retrieves the DataSourceView object from the 
        ' IDataSource associated with the data-bound control.            
        GetData().Select(CreateDataSourceSelectArguments(), _
            AddressOf OnDataSourceViewSelectCallback)

        ' The PerformDataBinding method has completed.
        RequiresDataBinding = False

        ' Raise the DataBound event.

    End Sub 'PerformSelect

    Private Sub OnDataSourceViewSelectCallback(ByVal retrievedData As IEnumerable)

        ' Call OnDataBinding only if it has not already
        ' been called in the PerformSelect method.
        If IsBoundUsingDataSourceID Then
        End If
        ' The PerformDataBinding method binds the data in the retrievedData 
        ' collection to elements of the data-bound control.

    End Sub 'OnDataSourceViewSelectCallback

    Protected Overrides Sub PerformDataBinding(ByVal retrievedData As IEnumerable)

        ' 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)
                    Dim props As PropertyDescriptorCollection = _

                    ' 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

            Next dataItem
        End If

    End Sub 'PerformDataBinding

    Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)

        ' Render nothing if the control is empty.            
        If BoxSet.Count <= 0 Then
        End If

        ' Make sure the control is declared in a form tag with runat=server.
        If Not (Page Is Nothing) Then
        End If

        ' For this example, render the BoxSet as 
        ' an unordered list of TextBox controls.            

        Dim item As Object
        For Each item In BoxSet

            Dim box As TextBox = CType(item, TextBox)

            ' Write each element as 
            ' <li><input type="text" value="string"><input/></li>
            writer.WriteAttribute("type", "text")
            writer.WriteAttribute("value", box.Text)
        Next item


    End Sub 'Render
    End Class 'TextBoxSet 
End Namespace

The following code example demonstrates how to use the TextBoxSet control, defined in the previous example, and bind it to an AccessDataSource control.

<%@Page language="VB" %>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.Controls.VB" 
    Assembly="Samples.AspNet.Controls.VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="" >
    <title>TextBoxSet Data-Bound Control - VB Example</title>

    <form id="Form1" method="post" runat="server">

          datasourceid="AccessDataSource1" />

          selectcommand="SELECT lastname FROM Employees" />

  • AspNetHostingPermission  for operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.
  • AspNetHostingPermission  for operating in a hosted environment. Demand value: InheritanceDemand; Permission value: Minimal.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions