0

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

4
  • u want to implement this thing on code behind? Commented Apr 6, 2010 at 9:35
  • How could you do what exactly? I don't understand what the problem with this is. Commented Apr 6, 2010 at 9:35
  • 1
    My guess is that he wants to implement this with his own controls. Like this: <custom:Parent ID="MyCustomParent" runat="server"><custom:Child ID="MyCustomChild1" runat="server" /><custom:Child ID="MyCustomChild2" runat="server" /></custom:Parent> Commented Apr 6, 2010 at 9:42
  • Hi, Kristof Claes, you're right. Commented Apr 6, 2010 at 9:46

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Brian Actually, I'm developing a tree control, but for some reason, I should not make it derive from TreeNode, let alone the ListControl. The example in the post is just for simplifying the complexity of the ploblem. :)
0

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.

3 Comments

Hi, is there a simple way(for example, I only need to apply some attributes to the properties or types) to make it work in code view? I don't need the complete designer support. Thanks:)
@Dylan use the DesignerSerializationVisibility and PersistenceMode attributes on the necessary property.
I used [PersistenceMode(PersistenceMode.InnerProperty)] and [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] on the ListItems(a collection) property of ListBox control in question. And I got the intellisence, but when I compile the code(calling code), the compiler said: Type 'ListItemCollection' does not have a public property named 'ListItem'.
0
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>");

        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.