现在想自定义一个TextBox,每个TextBox后有一串文字说明,表明此TextBox是用来做什么的,
因为考虑到有可能用户对此说明需要格式化,比如修改此文字大小、颜色、CSS……所以,考虑此文字说明使用一个Label动态添加上去。但我始终没有把它搞定,求各位帮忙。
我目前是这样弄的,重载了CreateChildControls,此Label不显示,把这段代码放在Render中也不显示,不知道如何弄了。
protected override void CreateChildControls()
{
this.Controls.Clear();
this.lblErr = new Label();
lblErr.ID =   this.ID + "_ErrorMessage";
lblErr.ApplyStyle( ErrMsgStyle ) ;
lblErr.Text =  ErrorMessage;
lblErr.Visible = ErrMsgVisible;
this.Controls.Add(lblErr)   ;
}

解决方案 »

  1.   

    Imports System.ComponentModel
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.Web.HttpContext
    Imports System.Web
    Imports System.Drawing.Design<DefaultProperty("CodeContent"), DefaultEvent("CodeTextBoxEnterReturn"), _
    ParseChildren(False), PersistChildren(False), _
    ToolboxData("<{0}:TextLabel runat=server></{0}:TextLabel>")> _
    Public Class TextLabel
        Inherits WebControl
        Implements INamingContainer    Private conLable As Label
        Private WithEvents conTextBox As TextBox
        Private htmlTable As Table
        Private htmlRow As TableRow
        Private mID As String    Public Overrides Property ID() As String
            Get
                mID = IIf(viewstate("id") Is Nothing, MyBase.ID, viewstate("id"))
                conLable.ID = mID & "_Label"
                conTextBox.ID = mID & "_TextBox"
                Return mID
            End Get
            Set(ByVal Value As String)
                viewstate("id") = Value
            End Set
        End Property    <NotifyParentProperty(True)> _
        Public Overrides Property BackColor() As System.Drawing.Color
            Get
                Return MyBase.BackColor
            End Get
            Set(ByVal Value As System.Drawing.Color)
                MyBase.BackColor = Value
            End Set
        End Property    <NotifyParentProperty(True)> _
        Public Overrides Property BorderColor() As System.Drawing.Color
            Get
                Return MyBase.BorderColor
            End Get
            Set(ByVal Value As System.Drawing.Color)
                MyBase.BorderColor = Value
            End Set
        End Property    <NotifyParentProperty(True)> _
        Public Overrides Property BorderStyle() As BorderStyle
            Get
                Return MyBase.BorderStyle
            End Get
            Set(ByVal Value As BorderStyle)
                MyBase.BorderStyle = Value
            End Set
        End Property    <NotifyParentProperty(True)> _
        Public Overrides Property BorderWidth() As Unit
            Get
                Return MyBase.BorderWidth
            End Get
            Set(ByVal Value As Unit)
                MyBase.BorderWidth = Value
            End Set
        End Property    <NotifyParentProperty(True)> _
        Public Overrides Property ForeColor() As System.Drawing.Color
            Get
                Return MyBase.ForeColor
            End Get
            Set(ByVal Value As System.Drawing.Color)
                MyBase.ForeColor = Value
            End Set
        End Property    <NotifyParentProperty(True)> _
        Public Overrides Property Width() As Unit
            Get
                Return MyBase.Width
            End Get
            Set(ByVal Value As Unit)
                MyBase.Width = Value
            End Set
        End Property    <NotifyParentProperty(True)> _
        Public Overrides Property Height() As Unit
            Get
                Return MyBase.Height
            End Get
            Set(ByVal Value As Unit)
                MyBase.Height = Value
            End Set
        End Property    <NotifyParentProperty(True)> _
        Public Overrides Property Enabled() As Boolean
            Get
                Return MyBase.Enabled
            End Get
            Set(ByVal Value As Boolean)
                MyBase.Enabled = Value
            End Set
        End Property    <NotifyParentProperty(True)> _
        Public Overrides Property Visible() As Boolean
            Get
                Return MyBase.Visible
            End Get
            Set(ByVal Value As Boolean)
                MyBase.Visible = Value
            End Set
        End Property    <NotifyParentProperty(True)> _
           Public Overrides Property CssClass() As String
            Get
                Return MyBase.CssClass
            End Get
            Set(ByVal Value As String)
                MyBase.CssClass = Value        End Set
        End Property    <NotifyParentProperty(True), PersistenceMode(PersistenceMode.InnerProperty), _
               Category("ChildControl"), Description("label")> _
           Public ReadOnly Property SubKosyouLabel() As Label
            Get
                Me.EnsureChildControls()
                Return conLable
            End Get
        End Property    <NotifyParentProperty(True), PersistenceMode(PersistenceMode.InnerProperty), _
            Category("ChildControl"), Description("textbox")> _
        Public ReadOnly Property SubCodeTextBox() As TextBox
            Get
                Me.EnsureChildControls()
                Return conTextBox
            End Get
        End Property    Private Function IsDesignMode() As Boolean
            If (HttpContext.Current Is Nothing) Then
                Return True
            Else
                Return False
            End If
        End Function    Protected Overrides Sub CreateChildControls()
            If conLable Is Nothing Then
                conLable = New Label
                conLable.Width = Unit.Pixel(100)
                conLable.Text = "Information"
            End If        If conTextBox Is Nothing Then
                conTextBox = New TextBox
                conTextBox.Width = Unit.Pixel(120)
            End If
            Call SetChildLayout()
        End Sub    Public Sub SetChildLayout()
            htmlTable = New Table
            htmlRow = New TableRow        htmlTable.Rows.Add(htmlRow)
            htmlRow.Cells.Add(New TableCell)
            htmlRow.Cells(0).VerticalAlign = VerticalAlign.Bottom
            htmlRow.Cells(0).HorizontalAlign = HorizontalAlign.Left
            htmlRow.Cells(0).Controls.Add(conTextBox)        htmlRow.Cells.Add(New TableCell)
            htmlRow.Cells(1).VerticalAlign = VerticalAlign.Bottom
            htmlRow.Cells(1).HorizontalAlign = HorizontalAlign.Left
            htmlRow.Cells(1).Controls.Add(conLable)        MyBase.Controls.Add(htmlTable)
        End Sub    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
            Me.EnsureChildControls()
            If IsDesignMode() Then
                MyBase.Controls.Clear()
                Call SetChildLayout()
            End If
            MyBase.Render(writer)
        End Sub
    End Class
      

  2.   

    几个月没碰过.NET了,来学习一下.
      

  3.   

    你现在是从WebControl继承的,这样操作可能比较多,可不可以直接从TextBox继承呢??
      

  4.   

    你做的本来就是复合控件,啊,你从TextBox继承还想加LABEL,你只有在RENDER中加抛两次,再第二次的时候不是加TEXTBOX而是加LABEL
      

  5.   

    如果这样话,我还是从TextBox继承,写一个静态的Label放在它的后面吧,这样可能还方便一些,谢谢你哦。。 rickjelly2004(rick & jelly)