ASP.NET自定义控件-允许文字内容的模板

ASP.NET自定义控件-允许文字内容的模板,asp.net,vb.net,user-controls,Asp.net,Vb.net,User Controls,我希望我的用户控件能够包含文字内容。例如: <fc:Text runat="server">Please enter your login information:</fc:Text> 此控件可以有文本以外的其他内容,因此将content属性设置为属性并不能解决我的问题 我在一些地方发现,我需要实现一个ControlBuilder类,以及另一个实现IParserAccessor的类 无论如何,我只希望默认的“Content”属性中包含所有类型的控件,包括文本控件和实际控

我希望我的用户控件能够包含文字内容。例如:

<fc:Text runat="server">Please enter your login information:</fc:Text>
此控件可以有文本以外的其他内容,因此将content属性设置为属性并不能解决我的问题

我在一些地方发现,我需要实现一个ControlBuilder类,以及另一个实现IParserAccessor的类


无论如何,我只希望默认的“Content”属性中包含所有类型的控件,包括文本控件和实际控件。

您需要将
ParseChildren
属性设置为“False”,否则控件中的任何内容都将解析为“Content”属性。这不能满足您的需要,因为您希望有控件和内容

为此,请重写该方法。检查已解析的控件类型,如果它是文本控件,则将其添加到UserControl的
Content
属性中。如果是其他控件,只需像往常一样将其添加到
控件
集合中即可

解析完所有子对象后,只需在文本控件或面板或其他地方显示
Content
属性

<ParseChildren(True, "Content")> _
Partial Public Class ctrFormText
    Inherits UserControl

    Private _content As ArrayList

    <PersistenceMode(PersistenceMode.InnerDefaultProperty), _
    DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _
    TemplateInstance(TemplateInstance.Single)> _
    Public Property Content() As ArrayList
        Get
            If _content Is Nothing Then
                Return New ArrayList
            End If
            Return _content
        End Get
        Set(ByVal value As ArrayList)
            _content = value
        End Set
    End Property

    Protected Overrides Sub CreateChildControls()
        If _content IsNot Nothing Then
            ctrChildren.Controls.Clear()
            For Each i As Control In _content
                ctrChildren.Controls.Add(i)
            Next
        End If
        MyBase.CreateChildControls()
    End Sub
End Class
Parser Error Message: Literal content ('Please enter your login information to access CKMS:') is not allowed within a 'System.Collections.ArrayList'.