views:

45

answers:

2

Hi,

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

A: 

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.

Paulo Santos
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 Lin
@Dylan use the `DesignerSerializationVisibility` and `PersistenceMode` attributes on the necessary property.
Paulo Santos
@Paulo SantosI 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'.
Dylan Lin
+1  A: 

Hey,

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.

Brian
@BrianActually, 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. :)
Dylan Lin