开发者

Reusable Custom Control with Properties

开发者 https://www.devze.com 2022-12-11 15:29 出处:网络
I have built a Custom Control Class that makes it much easier to build a side bar element in my html.

I have built a Custom Control Class that makes it much easier to build a side bar element in my html.

The problem I am running into is that when I set the "Text" property, the last time I set it gets used for every instance of the control on my page. This is my first time doing this, so I'm assuming I'm missing something basic.

Namespace CustomControl
Public Class SideBarElement : Inherits Literal

    ''' <summary>
    ''' Create Copyright Label
    ''' </summary>
    ''' <remarks></remarks>
    Protected Overrides Sub CreateChildControls()
        MyBase.Text = RenderHTML()
        MyBase.CreateChildControls()
    End Sub

    ''' <summary>
    ''' Set all copyright information.
    ''' </summary>
    Public Shared Function RenderHTML() As String
        Dim val As String


        val = "<div class=""side-bar-container"">" & _
                  "<div class=""side-bar-top"">" & _
                  "</div>" & _
                 " <div class=""side-bar-content"">" & _
                 _Text & _
                  "</div>" & _
                  "<div class=""side-bar-bottom"">" & _
                  "</div>" & _
              "</div>"

        Return val开发者_StackOverflow社区
    End Function

    ''' <summary>
    ''' Create Text Property
    ''' </summary>
    ''' <remarks></remarks>
    Private Shared _Text As String = String.Empty
    Public Shadows Property Text() As String
        Get
            Return _Text
        End Get
        Set(ByVal value As String)
            _Text = value
        End Set
    End Property
End Class

End Namespace

Using this control is supposed to look like this.

<sidebar:SideBarElement ID="SideBarElement1" runat="server">
 Bla Bla</br>My Fun Content.</sidebar:SideBarElement>

Any help will be greatly appreciated.


Removing the Text and _Text properties solved the problem. The literal control already has those properties built in, so I don't need to override them.

Namespace CustomControl
Public Class SideBarElement : Inherits Literal

    ''' <summary>
    ''' Create Copyright Label
    ''' </summary>
    ''' <remarks></remarks>
    Protected Overrides Sub CreateChildControls()
        MyBase.Text = "<div class=""side-bar-container"">" & _
                            "<div class=""side-bar-top""></div>" & _
                            "<div class=""side-bar-content"">" & _
                                Text & _
                            "</div>" & _
                      "</div>"
        MyBase.CreateChildControls()
    End Sub
End Class

End Namespace

0

精彩评论

暂无评论...
验证码 换一张
取 消