Search code examples
c#asp.netcollectionscustom-controlsdesigner

How to give a custom ASP.NET control the ability to parse XML markup to a collection?


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.


Solution

  • 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!