Export (0) Print
Expand All

CompositeDataBoundControl.CreateChildControls Method (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.

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

'Declaration
Protected MustOverride Function CreateChildControls ( _
	dataSource As IEnumerable, _
	dataBinding As Boolean _
) As Integer

Parameters

dataSource
Type: System.Collections.IEnumerable

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

dataBinding
Type: System.Boolean

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

Return Value

Type: System.Int32
The number of items created by the CreateChildControls(IEnumerable, Boolean).

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.

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

Imports System
Imports System.Collections
Imports System.Data.Common
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace Samples.AspNet.VB

    Public Class SimpleSpreadsheetControl
        Inherits CompositeDataBoundControl

        Protected table As New Table()

        Public Overridable ReadOnly Property Rows() As TableRowCollection
            Get 
                Return table.Rows
            End Get 
        End Property 


        Protected Overrides Function CreateChildControls(ByVal dataSource As IEnumerable, ByVal dataBinding As Boolean) As Integer 

            Dim count As Integer = 0
            ' If dataSource is not Nothing, iterate through it and 
            ' extract each element from it as a row, then 
            ' create a SimpleSpreadsheetRow and add it to the 
            ' rows collection. 
            If Not (dataSource Is Nothing) Then 

                Dim row As SimpleSpreadsheetRow
                Dim e As IEnumerator = dataSource.GetEnumerator()

                While e.MoveNext()
                    Dim datarow As Object = e.Current
                    row = New SimpleSpreadsheetRow(count, datarow)
                    Me.Rows.Add(row)
                    count += 1
                End While

                Controls.Add(table)
            End If 
            Return count
        End Function 'CreateChildControls
    End Class 'SimpleSpreadsheetControl


    Public Class SimpleSpreadsheetRow
        Inherits TableRow
        Implements IDataItemContainer

        Private dataObj As Object 
        Private _itemIndex As Integer 

        Public Sub New(ByVal itemIndex As Integer, ByVal o As Object)
            dataObj = o
            _itemIndex = itemIndex
        End Sub 'New 

        Public Overridable ReadOnly Property Data() As Object 
            Get 
                Return dataObj
            End Get 
        End Property 

        ReadOnly Property DataItem() As Object Implements IDataItemContainer.DataItem
            Get 
                Return Data
            End Get 
        End Property 

        ReadOnly Property DataItemIndex() As Integer Implements IDataItemContainer.DataItemIndex
            Get 
                Return _itemIndex
            End Get 
        End Property 

        ReadOnly Property DisplayIndex() As Integer Implements IDataItemContainer.DisplayIndex
            Get 
                Return _itemIndex
            End Get 
        End Property 
        Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)

            If Not (Data Is Nothing) Then 
                If TypeOf Data Is System.Data.Common.DbDataRecord Then 
                    Dim temp As DbDataRecord = CType(Data, DbDataRecord)
                    Dim i As Integer 

                    While i < temp.FieldCount
                        writer.Write("<TD>")
                        writer.Write(temp.GetValue(i).ToString())
                        writer.Write("</TD>")
                        i += 1
                    End While 
                Else
                    writer.Write(("<TD>" + Data.ToString() + "</TD>"))
                End If 

            Else
                writer.Write("<TD>This is a test</TD>")
            End If 
        End Sub 'RenderContents
    End Class 'SimpleSpreadsheetRow
End Namespace

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft