Creating the template control container class (VB) : TemplateContainer « Custom Controls « ASP.NET Tutorial






Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls

<DefaultProperty("Text")> _
<ToolboxData("<{0}:TemplatedControl runat=server></{0}:TemplatedControl>")> _
Public Class TemplatedControl
    Inherits System.Web.UI.WebControls.WebControl

    Private _name As String
    Private _text As String

    Private _message As Message
    Private _messageTemplate As ITemplate

    <Browsable(True)> Public ReadOnly Property Message() As Message
        Get
            EnsureChildControls()
            Return _message
        End Get
    End Property

    <PersistenceMode(PersistenceMode.InnerProperty), _
        TemplateContainer(GetType(Message))> _
    Public Property MessageTemplate() As ITemplate
        Get
            Return _messageTemplate
        End Get
        Set(ByVal value As ITemplate)
            _messageTemplate = value
        End Set
    End Property

    <Bindable(True), DefaultValue("")> Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    <Bindable(True), DefaultValue("")> Public Property Text() As String
        Get
            Return _text
        End Get
        Set(ByVal value As String)
            _text = value
        End Set
    End Property

    Public Overrides Sub DataBind()
        CreateChildControls()
        ChildControlsCreated = True
        MyBase.DataBind()
    End Sub

    Protected Overrides Sub CreateChildControls()

        Me.Controls.Clear()

        _message = New Message(Name, Text)

        Dim template As ITemplate = MessageTemplate
        template.InstantiateIn(_message)
        Controls.Add(_message)
    End Sub

End Class








14.14.TemplateContainer
14.14.1.Creating the template control container class (C#)
14.14.2.Creating the template control container class (VB)
14.14.3.Adding a templated control to a Web page (C#)
14.14.4.Adding a templated control to a Web page (VB)