多个控制集合我已经建立一个自定义的WebControl,它具有以下结构:渲染在ASP.NET自定义控件
<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属性,“内容”和“尾”。从来没有试图建立与多个控制集合控制,但解决它像这样(简化):
[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'的控件。
有些可以理解,因为标签出现在controlcollection的文本框之前。但是,使用默认的asp.net控件它确实有效,那么为什么这不起作用呢?我究竟做错了什么?在一个控件中是否可以有两个控件集合?我应该以不同的方式呈现吗
感谢您的回复。
是的,这将工作! – 2010-09-11 09:16:16