カスタム コンテナに問題があります。単一のコントロールをインスタンス化すると、インスタンス化されたコントロールの数が 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.Count
become 3. これですべてですが、1 が返されると期待しています。
私は何かを逃していますか?正確な子コントロールを取得したい場合はどうすればよいですか?
皆さん、ありがとうございました。