如何在ASP.NET中使用HtmlGenericControl创建段落(<p>)?</p>

时间:2008-12-24 14:17:09

标签: asp.net

如何创建一个pragraph&lt; p&gt;使用HtmlGenericControl类在ASP.NET中标记?

3 个答案:

答案 0 :(得分:27)

HtmlGenericControl para = new HtmlGenericControl ( "p" );

虽然我会把它留作额外属性/方法的容器控件。

HtmlContainerControl para = (HtmlContainerControl)new HtmlGenericControl ( "p" );

答案 1 :(得分:4)

我知道,也许不是最快的实现方式,但我曾经写过“丢失”的html控件,以后可以通过代码重用,而不会搞乱html标签。

Public Class HtmlParagraph
    Inherits HtmlControl

    Public Sub New()
        MyBase.New("p")
    End Sub

    Protected Overrides Sub AddParsedSubObject(ByVal obj As Object)
        If (TypeOf obj Is LiteralControl) Then
            Me._text = DirectCast(obj, LiteralControl).Text
        Else
            MyBase.AddParsedSubObject(obj)
        End If
    End Sub

    Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
        writer.RenderBeginTag(HtmlTextWriterTag.Fieldset)
        If (Me.HasControls) Then
            Me.RenderChildren(writer)
        ElseIf (Not String.IsNullOrEmpty(Me._text)) Then
            writer.Write(Me._text)
        End If
        writer.RenderEndTag()
    End Sub


    Private pText As String = String.Empty
    <Category("Appearance"), PersistenceMode(PersistenceMode.InnerDefaultProperty), Localizable(True), DefaultValue(""), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
    Public Overridable Property [Text]() As String
        Get
            If (Me.pText Is Nothing) Then Return String.Empty Else Return Me.pText
        End Get
        Set(ByVal value As String)
            Me.pText = value
        End Set
    End Property
End Class

答案 2 :(得分:-7)

new HtmlGenericControl("p");

PS。尝试使用Intellisense ...