简体   繁体   中英

How to create a Paragraph (<p>) using HtmlGenericControl in ASP.NET?

如何使用HtmlGenericControl类在ASP.NET中创建pragraph <p>标记?

HtmlGenericControl para = new HtmlGenericControl ( "p" );

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

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

I know, maybe is not the fastest way to implement, but I used to write "missing" html controls to be reusable later, via code, without messing around with html tags.

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
new HtmlGenericControl("p");

PS. Try using Intellisense...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM