Export (0) Print
Expand All

CompositeDataBoundControl Class

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

Represents the base class for a tabular data-bound control that is composed of other server controls.

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

public abstract class CompositeDataBoundControl : DataBoundControl, INamingContainer
public abstract class CompositeDataBoundControl extends DataBoundControl implements INamingContainer
public abstract class CompositeDataBoundControl extends DataBoundControl implements INamingContainer

The CompositeDataBoundControl class serves as the base class for a composite server control that binds to data from a data source. A composite server control is a control that is built from a combination of other server controls. Page developers do not use the CompositeDataBoundControl class directly; instead, they use controls that derive from this class. Examples of composite data-bound controls include the DetailsView, FormView, and GridView controls.

Classes that inherit from the CompositeDataBoundControl control must override the CreateChildControls(IEnumerable,Boolean) method to create the control hierarchy. To access the child controls of a composite data-bound control, use the Controls collection.

Notes to Inheritors When you inherit from the CompositeDataBoundControl class, you must override the following member: CreateChildControls(IEnumerable,Boolean).

The following code example demonstrates how to create a custom composite data-bound control that displays the values of a data source in a table.

using System;
using System.Collections;
using System.Data.Common;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Samples.AspNet.CS
{
    public class SimpleSpreadsheetControl : CompositeDataBoundControl
    {
        protected Table table = new Table();

        public virtual TableRowCollection Rows
        {
            get
            {
                return table.Rows;
            }
        }

        protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
        {

            int count = 0;
            // If dataSource is not null, iterate through it and
            // extract each element from it as a row, then
            // create a SimpleSpreadsheetRow and add it to the
            // rows collection.
            if (dataSource != null)
            {

                SimpleSpreadsheetRow row;
                IEnumerator e = dataSource.GetEnumerator();

                while (e.MoveNext())
                {
                    object datarow = e.Current;
                    row = new SimpleSpreadsheetRow(count, datarow);
                    this.Rows.Add(row);
                    ++count;
                }

                Controls.Add(table);
            }
            return count;
        }
    }

    //
    //
    public class SimpleSpreadsheetRow : TableRow, IDataItemContainer
    {
        private object data;
        private int _itemIndex;

        public SimpleSpreadsheetRow(int itemIndex, object o)
        {
            data = o;
            _itemIndex = itemIndex;
        }

        public virtual object Data
        {
            get
            {
                return data;
            }
        }
        object IDataItemContainer.DataItem
        {
            get
            {
                return Data;
            }
        }
        int IDataItemContainer.DataItemIndex
        {
            get
            {
                return _itemIndex;
            }
        }
        int IDataItemContainer.DisplayIndex
        {
            get
            {
                return _itemIndex;
            }
        }
        protected override void RenderContents(HtmlTextWriter writer)
        {

            if (Data != null)
            {
                if (Data is System.Data.Common.DbDataRecord)
                {
                    DbDataRecord temp = (DbDataRecord)Data;
                    for (int i = 0; i < temp.FieldCount; ++i)
                    {
                        writer.Write("<TD>");
                        writer.Write(temp.GetValue(i).ToString());
                        writer.Write("</TD>");
                    }
                }
                else
                    writer.Write("<TD>" + Data.ToString() + "</TD>");
            }

            else
                writer.Write("<TD>This is a test</TD>");
        }
    }
}

System.Object
   System.Web.UI.Control
     System.Web.UI.WebControls.WebControl
       System.Web.UI.WebControls.BaseDataBoundControl
         System.Web.UI.WebControls.DataBoundControl
          System.Web.UI.WebControls.CompositeDataBoundControl
             System.Web.UI.WebControls.DetailsView
             System.Web.UI.WebControls.FormView
             System.Web.UI.WebControls.GridView

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

Community Additions

ADD
Show:
© 2014 Microsoft