Allowing a Server Control to Contain Custom Child Collection

I’ve probably done this at least a dozen times, but when you don’t do something everyday, you tend to forget. I created a custom control that I wanted to place into a control collection. So, I created the child control as a custom server control. Then I created another server control that contained a property called Items as a Generic.List(Of T) where T was my custom child control. I kept running my app and couldn’t figure out why I couldn’t create my child controls in the Parent. I forgot that I had to import the namespace System.ComponentModel and add the PersistenceMode property to the property. So, my code looked like this:

Visual Basic

Public Class MyServerControlCollection
Inherits WebControl

Private _Items As Generic.List(Of MyChildControl)

    <PersistenceMode(PersistenceMode.InnerProperty)> _
    Public Property Items() As Generic.List(Of MyChildControl)
End Get
(ByVal value As Generic.List(Of MyChildControl))
End Set
    End Property

    Protected Overrides Sub 
For Each obj As MyChildControl In Items

End Sub

End Class


public class MyServerControlCollection : WebControl

private Generic.List<MyChildControl> _Items;

Generic.List<MyChildControl> Items {
get return _Items}
set { _Items = value; }

protected override void CreateChildControls()
foreach (MyChildControl obj in Items) {



Hope that helps!

Leave a Reply

Your email address will not be published. Required fields are marked *