次の構造を持つカスタム WebControl を作成しました。
<gws:ModalBox ID="ModalBox1" HeaderText="Title" runat="server">
<Contents>
<asp:Label ID="KeywordLabel" AssociatedControlID="KeywordTextBox" runat="server">Keyword: </asp:Label><br />
<asp:TextBox ID="KeywordTextBox" Text="" runat="server" />
</Contents>
<Footer>(controls...)</Footer>
</gws:ModalBox>
コントロールには、'Contents' と 'Footer' の 2 つの ControlCollection プロパティが含まれています。複数のコントロール コレクションでコントロールを構築しようとしたことはありませんが、次のように解決しました (簡略化):
[PersistChildren(false), ParseChildren(true)]
public class ModalBox : WebControl
{
private ControlCollection _contents;
private ControlCollection _footer;
public ModalBox()
: base()
{
this._contents = base.CreateControlCollection();
this._footer = base.CreateControlCollection();
}
[PersistenceMode(PersistenceMode.InnerProperty)]
public ControlCollection Contents { get { return this._contents; } }
[PersistenceMode(PersistenceMode.InnerProperty)]
public ControlCollection Footer { get { return this._footer; } }
protected override void RenderContents(HtmlTextWriter output)
{
// Render content controls.
foreach (Control control in this.Contents)
{
control.RenderControl(output);
}
// Render footer controls.
foreach (Control control in this.Footer)
{
control.RenderControl(output);
}
}
}
ただし、適切にレンダリングされているようですが、プロパティ内にasp.netラベルと入力コントロールを追加すると機能しなくなります(上記のasp.netコードを参照)。私はHttpExceptionを取得します:
ラベル 'KeywordLabel' に関連付けられている ID 'KeywordTextBox' のコントロールが見つかりません。
コントロールコレクションのテキストボックスの前にラベルが表示されるため、ある程度理解できます。ただし、デフォルトのasp.netコントロールでは機能しますが、なぜこれが機能しないのですか? 私は何を間違っていますか?1 つのコントロールに 2 つのコントロール コレクションを含めることさえ可能ですか? 別の方法でレンダリングする必要がありますか?
返信ありがとうございます。