DataBoundControl Class

Note: This class is new in the .NET Framework version 2.0.

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 abstract class DataBoundControl : BaseDataBoundControl
public abstract class DataBoundControl extends BaseDataBoundControl
public abstract class DataBoundControl extends BaseDataBoundControl

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.

using System;
using System.Collections;
using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Samples.AspNet.Controls.CS {

    public class TextBoxSet : DataBoundControl {

        private IList alBoxSet;
        public IList BoxSet {
            get {
                if (null == alBoxSet) {
                     alBoxSet = new ArrayList();
                return alBoxSet;                
        public string DataTextField {
            get {
                object o = ViewState["DataTextField"];
                return((o == null) ? string.Empty : (string)o);
            set {
                ViewState["DataTextField"] = value;
                if (Initialized) {
        protected override void PerformSelect() {            

           // Call OnDataBinding here if bound to a data source using the
           // DataSource property (instead of a DataSourceID), because the
           // databinding statement is evaluated before the call to GetData.       
            if (! IsBoundUsingDataSourceID) {
            // The GetData method retrieves the DataSourceView object from  
            // the IDataSource associated with the data-bound control.            
            // The PerformDataBinding method has completed.
            RequiresDataBinding = false;
            // Raise the DataBound event.
        private void OnDataSourceViewSelectCallback(IEnumerable retrievedData) {
           // Call OnDataBinding only if it has not already been 
           // called in the PerformSelect method.
            if (IsBoundUsingDataSourceID) {
            // The PerformDataBinding method binds the data in the  
            // retrievedData collection to elements of the data-bound control.
        protected override void PerformDataBinding(IEnumerable 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 = 

                        // 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();
        protected override void Render(HtmlTextWriter writer) {

            // Render nothing if the control is empty.            
            if (BoxSet.Count <= 0) {

            // Make sure the control is declared in a form tag 
            // with runat=server.
            if (Page != null) {
            // For this example, render the BoxSet as 
            // an unordered list of TextBox controls.            

            foreach (object item in BoxSet) {
                TextBox box = (TextBox) item;
                // Write each element as 
                // <li><input type="text" value="string"><input/></li>
                writer.WriteAttribute("type", "text");
                writer.WriteAttribute("value", box.Text);


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="c#" %>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.Controls.CS" 
    Assembly="Samples.AspNet.Controls.CS" %>

    <title>TextBoxSet Data-Bound Control  - C# 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 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0