How to create a Paragraph (<p>) using HtmlGenericControl in ASP.NET?
23,501
Solution 1
HtmlGenericControl para = new HtmlGenericControl ( "p" );
Although I would leave it as a container control for the extra properties/methods.
HtmlContainerControl para = (HtmlContainerControl)new HtmlGenericControl ( "p" );
Solution 2
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
Related videos on Youtube
Author by
Nikola Stjelja
I'm a father , husband, developer and writer. I also love games, books and tv show and movies. And walking and probalbly something else..
Updated on October 01, 2020Comments
-
Nikola Stjelja over 3 years
How to create a pragraph <p> tag in ASP.NET using the HtmlGenericControl class?
-
mberube.Net over 14 yearsHtmlContainerControl is an abstract class. I think it should be : HtmlContainerControl para = new HtmlGenericControl("p"). Thank, you put me on the right way to find answer to my problem.
-
tvanfosson over 14 yearsI turned those around. Fixed.
-
Daniel Ballinger over 13 yearsI think you mean writer.RenderBeginTag(HtmlTextWriterTag.P) rather than Fieldset.
-
gius over 9 yearsCould someone please explain the reason for all the downvotes?