【发布时间】:2011-01-27 16:42:15
【问题描述】:
我已经构建了一个自定义 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>
该控件包含两个 ControlCollection 属性,“Contents”和“Footer”。从来没有尝试用多个控件集合构建一个控件,但是这样解决(简化):
[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:
无法找到 ID 为“KeywordTextBox”的控件 与标签“KeywordLabel”相关联。
有点理解,因为标签出现在控件集合中的文本框之前。但是,使用默认的 asp.net 控件它确实可以工作,那么为什么这不起作用呢?我究竟做错了什么?甚至可以在一个控件中有两个控件集合吗?我应该用不同的方式渲染它吗?
感谢您的回复。
【问题讨论】:
标签: c# asp.net custom-controls rendering controlcollection