Public Class FormList
    Inherits Repeater

    Property AddText As String
        Get
            Return tmpFooter.btnAdd.Text
        End Get
        Set(value As String)
            tmpFooter.btnAdd.Text = value
        End Set
    End Property

    Property TargetCount As Integer
        Get
            Return tmpHeader.hidCount.Value
        End Get
        Set(value As Integer)
            tmpHeader.hidCount.Value = value
        End Set
    End Property

    Event AddClick(sender As Object, e As EventArgs)


    Private Class FormListHeaderTemplate
        Implements ITemplate

        Public hidCount As New HiddenField()

        Sub New()
            hidCount.Value = 1
        End Sub

        Public Sub InstantiateIn(container As Control) Implements ITemplate.InstantiateIn
            Dim ph As New PlaceHolder()
            ph.Controls.Add(New LiteralControl("<section class='FormList'>"))
            container.Controls.Add(ph)
        End Sub

    End Class
    Private tmpHeader As New FormListHeaderTemplate()

    Private Class FormListFooterTemplate
        Implements ITemplate

        Public btnAdd As New Button()

        Sub New()
            btnAdd.CausesValidation = False
            btnAdd.ID = "btnAdd"
        End Sub

        Public Sub InstantiateIn(container As Control) Implements ITemplate.InstantiateIn
            Dim ph As New PlaceHolder()
            ph.Controls.Add(btnAdd)
            ph.Controls.Add(New LiteralControl("</section>"))
            container.Controls.Add(ph)
        End Sub

    End Class
    Private tmpFooter As New FormListFooterTemplate()


    Sub New()
        HeaderTemplate = tmpHeader
        FooterTemplate = tmpFooter
        AddHandler tmpFooter.btnAdd.Click, AddressOf btnAdd_Click
    End Sub

    Protected Overrides Sub LoadViewState(savedState As Object)

        tmpFooter.btnAdd.CausesValidation = False

        Dim state = CType(savedState, Object())
        MyBase.LoadViewState(state(0))
        tmpHeader.hidCount.Value = state(1)

    End Sub

    Protected Overrides Function SaveViewState() As Object
        Dim state(2) As Object
        state(0) = MyBase.SaveViewState()
        state(1) = tmpHeader.hidCount.Value
        Return state
    End Function

    Private Sub btnAdd_Click(sender As Object, e As EventArgs)
        RaiseEvent AddClick(sender, e)
    End Sub

End Class