Recommended hosting
Oct 30 2006

Server control - custom collection with innercontent

Posted by admin under Controls

Continued from this article

What if we want something like this:



<jquery:FooBarHolderControl ID="idHolder" runat="server">
<Bars>
<jquery:Bar Text="Pekka" />
<jquery:Bar Text="Another" />
</Bars>
    <Foos>
        <jquery:Foo Position="Whatever">foo123</jquery:Foo>
    </Foos>
</jquery:FooBarHolderControl>

The difference now is that the Name property in object Foo is read from innercontent:

<jquery:Foo Position="Whatever">foo123</jquery:Foo>

i.e foo123 is the name.

And the changes needed in the code are not too much - only hard to figure out:

(only showing class Foo):



    [
    ParseChildren(true, "Name"),
    DefaultProperty("Name")]
    public class Foo
    {
        private string m_strName = "foo123";

        [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public string Name
        {
            get { return m_strName; }
            set { m_strName = value; }
        }
        private string m_strPosition = "Whatever";

        public string Position
        {
            get { return m_strPosition; }
            set { m_strPosition = value; }
        }
    }

ParseChildren(true, "Name"), means specifying that the innercontent should be read into the Name property. Secondly we add  the attribute  [PersistenceMode.PersistenceMode.InnerDefaultProperty)] to the Name property so the designer will know how to serialize the changed content.