Question

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 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:)

Was it helpful?

Solution

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.

OTHER TIPS

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.

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>");

        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top