다음을 통해 공유


Control.ChildControlsCreated 속성

정의

서버 컨트롤의 자식 컨트롤이 만들어졌는지 여부를 나타내는 값을 가져옵니다.

protected:
 property bool ChildControlsCreated { bool get(); void set(bool value); };
protected bool ChildControlsCreated { get; set; }
member this.ChildControlsCreated : bool with get, set
Protected Property ChildControlsCreated As Boolean

속성 값

Boolean

자식 컨트롤이 만들어졌으면 true이고, 그렇지 않으면 false입니다.

예제

다음 예제에서는 사용자 지정 Repeater 컨트롤의 메서드 재정 OnDataBinding 의를 보여 줍니다. 데이터 바인딩이 다시 ChildControlsCreated 발생할 때까지 자식 컨트롤이 만들어지지 않도록 하려면 개체를 만들고 컨트롤의 ControlCollection 개체에 추가한 후 RepeaterItem 속성이 설정 true 됩니다.

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

namespace TemplateControlSamples
{
    [ParseChildren(true)]
    public class CustomRepeater : Repeater
    {

        // Override to prevent LiteralControls from being added as children.
        protected override void AddParsedSubObject(object o)
        {
        }

        // Override to create repeated items.
        protected override void CreateChildControls()
        {
            object o = ViewState["NumItems"];
            if (o != null)
            {
                // Clear any existing child controls.
                Controls.Clear();

                int numItems = (int)o;
                for (int i = 0; i < numItems; i++)
                {
                    // Create an item.
                    RepeaterItem item = new RepeaterItem(i, ListItemType.Item);
                    // Initialize the item from the template.
                    ItemTemplate.InstantiateIn(item);
                    // Add the item to the ControlCollection.
                    Controls.Add(item);
                }
            }
        }

        // Override to create the repeated items from the DataSource.
        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);
            if (DataSource != null)
            {
                // Iterate over an ICollection DataSource, creating a new item for each data item.
                IEnumerator dataEnum = ((ICollection)base.DataSource).GetEnumerator();
                int i = 0;
                while (dataEnum.MoveNext())
                {
                    // Create an item.
                    RepeaterItem item = new RepeaterItem(i, ListItemType.Item);
                    item.DataItem = dataEnum.Current;
                    // Initialize the item from the template.
                    ItemTemplate.InstantiateIn(this);
                    // Add the item to the ControlCollection.
                    Controls.Add(item);

                    i++;
                }

                // Prevent child controls from being created again.
                ChildControlsCreated = true;
                // Store the number of items created in view state for postback scenarios.
                ViewState["NumItems"] = i;
            }
        }
    }
}
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

다음 예제에서는 웹 페이지에서 사용자 지정 반복기 컨트롤을 사용하는 방법을 보여 줍니다.

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

<script runat="server">


    protected void Page_Load(object sender, EventArgs e)
    {
        ArrayList a = new ArrayList();
        a.Add("data item 1");
        a.Add("data item 2");
        Repeater1.DataSource = a;
        Page.DataBind();
    }

</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:CustomRepeater ID="Repeater1" runat="server">
        <ItemTemplate><%# Container.DataItem %> <br /></ItemTemplate>
        </Samples:CustomRepeater>
    </div>
    </form>
</body>
</html>
<%@ 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>

적용 대상

추가 정보