在asp.net中没有显示自定义标签

时间:2010-12-14 06:37:48

标签: asp.net

我正在构建自定义的asp.net服务器控件。它用于设计时但没有显示运行时间。我写这个。

   <cc1:TemplateMenu ID="TemplateMenu1" runat="server">
        <HeaderTemplate>
            <asp:Label ID="Title" Text="Hello World" runat="server"></asp:Label>
        </HeaderTemplate>
        <SperatorTemplate>|</SperatorTemplate>
        <FooterTemplate>
            <asp:TextBox ID="TextBox" Text="" runat="server"></asp:TextBox>
            <asp:Button ID="Button" Text="Click Me"  runat="server"/>
        </FooterTemplate>
    </cc1:TemplateMenu>

这是设计时间。

这是服务器控制代码文件。

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports Yoma.Yoma.CustomControls.BasicTemplate

Namespace Yoma.CustomControls.TemplateMenu

    <DefaultProperty("Text"), ToolboxData("<{0}:TemplateMenu runat=server></{0}:TemplateMenu>")> _
    Public Class TemplateMenu
        Inherits CompositeControl

        Private MenuData As New ArrayList

        Public Sub New()

            MenuData = New ArrayList

            Dim MenuItem As New ListItem

            MenuItem.Text = "Apress"
            MenuItem.Value = "http://www.apress.com"
            MenuData.Add(MenuItem)

            MenuItem = New ListItem
            MenuItem.Text = "Microsoft"
            MenuItem.Value = "http://www.microsoft.com"
            MenuData.Add(MenuItem)

            MenuItem = New ListItem
            MenuItem.Text = "ASP.Net"
            MenuItem.Value = "http://asp.net"
            MenuData.Add(MenuItem)

        End Sub

#Region "HeaderTemplate"

        Private _HeaderTemplate As ITemplate = Nothing

        <Browsable(False), Description("The header template"), PersistenceMode(PersistenceMode.InnerProperty)> _
        Public Property HeaderTemplate() As ITemplate
            Get
                Return _HeaderTemplate
            End Get
            Set(ByVal value As ITemplate)
                _HeaderTemplate = value
            End Set
        End Property

#End Region

#Region "FooterTemplate"

        Private _FooterTemplate As ITemplate = Nothing

        <Browsable(False), Description("The footer template"), PersistenceMode(PersistenceMode.InnerProperty)> _
        Public Property FooterTemplate() As ITemplate
            Get
                Return _FooterTemplate
            End Get
            Set(ByVal value As ITemplate)
                _FooterTemplate = value
            End Set
        End Property

#End Region

#Region "SperatorTemplate"

        Private _SperatorTemplate As ITemplate = Nothing

        <Browsable(False), Description("The sperator template"), PersistenceMode(PersistenceMode.InnerProperty)> _
        Public Property SperatorTemplate() As ITemplate
            Get
                Return _SperatorTemplate
            End Get
            Set(ByVal value As ITemplate)
                _SperatorTemplate = value
            End Set
        End Property

#End Region

#Region "Create Control"

        Public Sub CreateControlHierarchy()

            If HeaderTemplate Is Nothing Then
                Dim Header As New BasicTemplateContainer

                HeaderTemplate.InstantiateIn(Header)
                Controls.Add(Header)
            End If

            Dim Count As Integer = MenuData.Count

            For Each Item As ListItem In MenuData
                Dim Link As New HyperLink

                Link.Text = Item.Text
                Link.NavigateUrl = Item.Value
                Controls.Add(Link)
                Controls.Add(New LiteralControl("|"))
            Next

            If FooterTemplate Is Nothing Then
                Dim Footer As New BasicTemplateContainer

                FooterTemplate.InstantiateIn(Footer)
                Controls.Add(Footer)
            End If

        End Sub

        Protected Overrides Sub CreateChildControls()
            Controls.Clear()
            CreateControlHierarchy()
        End Sub

        Public Overrides ReadOnly Property Controls() As ControlCollection
            Get
                EnsureChildControls()
                Return MyBase.Controls
            End Get
        End Property

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

#End Region

    End Class

End Namespace

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace Yoma.CustomControls.BasicTemplate

    <DefaultProperty("Text"), ToolboxData("<{0}:BasicTemplateContainer runat=server></{0}:BasicTemplateContainer>")> _
    Public Class BasicTemplateContainer
        Inherits WebControl
        Implements INamingContainer

        Public Sub New()
            Me.BorderWidth = 2
            Me.BorderStyle = WebControls.BorderStyle.Outset
        End Sub

    End Class

End Namespace

请帮助我......我遇到了一个真正的问题。

1 个答案:

答案 0 :(得分:1)

您是否从页面代码中为控件调用了DataBind方法?除非您这样做,否则可能无法创建控制树。