ASP.NET custom server control: nested items
In the aspx code view, we can code like this:
<asp:ListBox runat="server">
<asp:ListItem Text="Item1" />
<asp:ListItem Text="Item2" />
</asp:ListBox>
However, the ListItem class is not a server control. How could we do that in our custom server开发者_Go百科 control? That is, how to develop a ListItem-like class which can make this markup style works? I'm building a server control which is similar to the ListBox.
Thanks:)
Create a class that inherits from ListControl, for various reasons, even this may not give you complete UI intellisense for this (something about the way the designer works with custom assemblies), but it will support adding list items and manage that all for you.
If you want to do such thin with your control you need to implement a Control Designer.
Edited to add:
And also check the ParseChildren
, PersistChildren
, DesignerSerializationVisibility
, and PersistenceMode
attributes.
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>");
}
精彩评论