views:

23

answers:

2

I'm writing a custom ASP.NET webcontrol and would like it to have a collection of custom items which can also be specified in the XML markup. Something like this:

class MyControl: WebControl
{
    public IList<MyItemType> MyItems { get; private set; }
}

And in the markup:

<asd:MyControl runat="server" id="mc1">
    <MyItems>
        <MyDerivedCustomItem asd="dsa"/>
        <MyOtherDerivedCustomItem asd="dsa"/>
    </MyItems>
</asd:MyControl>

How do I do this? I though this was all about implementing some interface on the collection or adding some special attributes to the property, but nothing I do seems to work.

A: 

Perhaps this MSDN article will give you a head start: http://msdn.microsoft.com/en-us/library/9txe1d4x.aspx

One of the important attributes to look at is:
[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
which will give the desired effect...

HTH.

Sunny
Sorry, I will have at least two such collections, so `InnerDefaultProperty` will not do. `InnerProperty` will, but that doesn't work as per se (though I managed to crack it, wait for my reply).
Vilx-
+1  A: 

Ha, got it!

Seems that IList<T> is a bad type to return. It needs to be something that implements plain old IList. Though the implementation can be explicit. Providing method overrides with the same names but strongly typed arguments will limit the available options in the markup editor.

Well, of course, adding the standard attributes also seems to be necessary:

[PersistenceMode(PersistenceMode.InnerProperty)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[MergableProperty(false)]

Not sure what they all do (the documentation is pretty nondescriptive in many places), but it works!

Vilx-