Asp.net 使用容器实例化控件

Asp.net 使用容器实例化控件,asp.net,asp.net-webcontrol,Asp.net,Asp.net Webcontrol,我的自定义容器有问题。 当我实例化单个控件时,实例化控件的计数变为3 这有点难解释,所以让我在代码中描述一下 此my child控件类代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace WebApplication2 { public class Child:System.Web.UI.Control { } } 这是MyC

我的自定义容器有问题。 当我实例化单个控件时,实例化控件的计数变为3

这有点难解释,所以让我在代码中描述一下 此my child控件类代码:

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

namespace WebApplication2
{
    public class Child:System.Web.UI.Control
    {
    }
}
这是MyContainer类别代码:

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);

        }
    }

}
这是我的aspx页面:

<%@ 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>

所以当我运行这一行时:
ChildControls.instantiationein(cl)我的
cl.Controls.Count
变为3。虽然这是我的全部 我希望它返回1

我错过什么了吗?如果我想得到精确的子控件,我应该怎么做


谢谢大家。

经过长时间的斗争,我终于找到了答案

在我的ChildControls模板中,我有2个
/r/n
,它们已转换为LiteralControl

所以我有2个新行和我的asp:textBox,所以它返回3