在 aspx代码视图中,我们可以这样编写代码:
<asp:ListBox runat="server">
<asp:ListItem Text="Item1" />
<asp:ListItem Text="Item2" />
</asp:ListBox>
但是,ListItem 类不是服务器控件。我们如何在自定义服务器控件中做到这一点?也就是说,如何开发一个类似 ListItem 的类来使这种标记样式起作用?我正在构建一个类似于 ListBox 的服务器控件。
谢谢:)
创建一个从 ListControl 继承的类,出于各种原因,即使这样也可能无法为您提供完整的 UI 智能感知(设计师使用自定义程序集的方式),但它将支持添加列表项并为您管理所有内容。
如果你想用你的控件做这么薄,你需要实现一个Control Designer。
编辑添加:
并检查ParseChildren、PersistChildren、DesignerSerializationVisibility和PersistenceMode属性。
protected override void RenderContents(HtmlTextWriter output)
{
output.Write("<div><div class=\"UserSectionHead\">");
Label l = new Label() { Text = Label };
TextBox t = new TextBox() { Text = Text };
l.AssociatedControlID = t.ID;
l.RenderControl(output);
output.Write("</div><div class=\"UserSectionBody\"><div class=\"UserControlGroup\"><nobr>");
t.RenderControl(output);
output.Write("</nobr></div></div><div style=\"width:100%\" class=\"UserDottedLine\"></div>");
}