0

カスタム コンテナに問題があります。単一のコントロールをインスタンス化すると、インスタンス化されたコントロールの数が 3 になります!!!

説明するのは少し難しいので、この子のコントロール クラス コードのコードで説明します。

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.InstantiateIn(cl);my cl.Controls.Countbecome 3. これですべてですが、1 が返されると期待しています。

私は何かを逃していますか?正確な子コントロールを取得したい場合はどうすればよいですか?

皆さん、ありがとうございました。

4

1 に答える 1

0

長い闘争の後、私はそれが何であるかを見つけました。

私のChildControlsテンプレートには2つ/r/nあり、それらはLit​​eralControlに変換されました

だから私は2つの新しい行と私のasp:textBoxを持っているので、3を返します.

于 2014-08-26T11:52:40.823 に答える