asp.netasp.net-webcontrol

InstantiateIn a control with Container


I have a problem with my custom container. when i Instantiate a single control the count of Instantiated control become 3!!!

it's little hard to explain so let me describe it in my code this my child Control Class Code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication2
{
    public class Child:System.Web.UI.Control
    {
    }
}

this is MyContainer Class code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

namespace WebApplication2
{
    public class MyContaner:System.Web.UI.WebControls.WebControl,INamingContainer
    {
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public ITemplate ChildControls { get; set; }

        protected override void CreateChildControls()
        {
           var cl = new Child();
           ChildControls.InstantiateIn(cl);
           var x=cl.Controls.Count;
           this.Controls.Add(cl);

        }
    }

}

and this is my aspx Page :

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>
<%@ Register Assembly="WebApplication2" Namespace="WebApplication2" TagPrefix="t" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>

<body>
    <form id="form1" runat="server">
    <div>

        <t:MyContainer runat="server">
            <ChildControls>
                <asp:TextBox runat="server"></asp:TextBox>
            </ChildControls>
        </t:MyContainer>


    </div>
    </form>
</body>
</html>

So When i run this line: ChildControls.InstantiateIn(cl); my cl.Controls.Count become 3. although it's all i have i expect it returns 1.

am i missing something? what should i do if i want to get exact child Controls?

thank you All.


Solution

  • after a Long Struggle i found what it is.

    in my ChildControls Template I have 2 /r/n and they have converted to LiteralControl

    so i have 2 new line and my asp:textBox so it returns 3.