Export (0) Print
Expand All

Control.ChildControlsCreated Property

Gets a value that indicates whether the server control's child controls have been created.

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

'Declaration
Protected Property ChildControlsCreated As Boolean

Property Value

Type: System.Boolean
true if child controls have been created; otherwise, false.

The following example demonstrates an override of the OnDataBinding method of a custom Repeater control. To ensure that child controls are not created until data binding occurs again, the ChildControlsCreated property is set to true after the RepeaterItem objects are created and added to the control's ControlCollection object.

Imports System
Imports System.Collections
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace TemplateControlSamplesVB

    <ParseChildren(True)> _
    Public Class CustomRepeaterVB : Inherits Repeater

        ' Override to prevent LiteralControls from being added as children. 
        Protected Overrides Sub AddParsedSubObject(ByVal O As Object)
        End Sub 

        ' Override to create repeated items. 
        Protected Overrides Sub CreateChildControls()
            Dim O As Object = ViewState("NumItems")
            If Not (O Is Nothing) Then 
                ' Clear any existing child controls.
                Controls.Clear()

                Dim i As Integer 
                Dim NumItems As Integer = CInt(O)
                For i = 0 To NumItems - 1
                    ' Create an item. 
                    Dim Item As RepeaterItem = New RepeaterItem(i, ListItemType.Item)
                    ' Initialize the item from the template.
                    ItemTemplate.InstantiateIn(Item)
                    ' Add the item to the ControlCollection.
                    Controls.Add(Item)
                Next 
            End If 
        End Sub 

        ' Override to create the repeated items from the DataSource. 
        Protected Overrides Sub OnDataBinding(ByVal E As EventArgs)
            MyBase.OnDataBinding(E)

            If Not DataSource Is Nothing Then 

                ' Iterate over the DataSource, creating a new item for each data item. 
                Dim DataEnum As IEnumerator = DataSource.GetEnumerator()
                Dim i As Integer = 0
                Do While (DataEnum.MoveNext())

                    ' Create an item. 
                    Dim Item As RepeaterItem = New RepeaterItem(i, ListItemType.Item)
                    ' Initialize the item from the template.
                    ItemTemplate.InstantiateIn(Item)
                    ' Add the item to the ControlCollection.
                    Controls.Add(Item)

                    i = i + 1
                Loop 

                ' Prevent child controls from being created again.
                ChildControlsCreated = True 
                ' Store the number of items created in view state for postback scenarios.
                ViewState("NumItems") = i
            End If 
        End Sub 
    End Class 

End Namespace

The following example demonstrates how to use the custom repeater control in a Web page.

<%@ Page Language="VB" %>
<%@ Register TagPrefix="Samples" Namespace="TemplateControlSamplesVB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim a As New ArrayList()
        a.Add("data item 1")
        a.Add("data item 2")
        Repeater1.DataSource = a
        Page.DataBind()

    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ChildControlsCreated Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <Samples:CustomRepeaterVB ID="Repeater1" runat="server">
        <ItemTemplate><%# Container.DataItem %> <br /></ItemTemplate>
        </Samples:CustomRepeaterVB>
    </div>
    </form>
</body>
</html>

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.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