CompositeDataBoundControl.CreateChildControls Method

Definition

Creates the control hierarchy that is used to render a composite data-bound control.

Overloads

CreateChildControls()

Creates the control hierarchy that is used to render a composite data-bound control based on the values that are stored in view state.

CreateChildControls(IEnumerable, Boolean)

When overridden in an abstract class, creates the control hierarchy that is used to render the composite data-bound control based on the values from the specified data source.

CreateChildControls()

Creates the control hierarchy that is used to render a composite data-bound control based on the values that are stored in view state.

C#
protected internal override void CreateChildControls();

Remarks

The CreateChildControls() method is a helper method that is used by classes that derive from the CompositeDataBoundControl class to create the control hierarchy for a composite data-bound control. This overload of the method creates the control hierarchy based on values from view state, rather than directly from the data source.

See also

Applies to

.NET Framework 4.8.1 a ďalšie verzie
Produkt Verzie
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

CreateChildControls(IEnumerable, Boolean)

When overridden in an abstract class, creates the control hierarchy that is used to render the composite data-bound control based on the values from the specified data source.

C#
protected abstract int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding);

Parameters

dataSource
IEnumerable

An IEnumerable that contains the values to bind to the control.

dataBinding
Boolean

true to indicate that the CreateChildControls(IEnumerable, Boolean) is called during data binding; otherwise, false.

Returns

The number of items created by the CreateChildControls(IEnumerable, Boolean).

Examples

The following code example demonstrates how to override the CreateChildControls(IEnumerable, Boolean) method in a custom control to create the control hierarchy.

C#
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>");
            }
        }
    }
}

Remarks

The CreateChildControls() method is a helper method that is used by classes that derive from the CompositeDataBoundControl class to create the control hierarchy for a composite data-bound control. When extending the CompositeDataBoundControl class, you must override the CreateChildControls() method to create your own control hierarchy. For more information on creating a composite control, see Developing Custom ASP.NET Server Controls.

See also

Applies to

.NET Framework 4.8.1 a ďalšie verzie
Produkt Verzie
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1