tags:

views:

2520

answers:

3

How to create a pragraph <p> tag in ASP.NET using the HtmlGenericControl class?

A: 
new HtmlGenericControl("p");

PS. Try using Intellisense...

gius
+4  A: 
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" );
tvanfosson
HtmlContainerControl 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.
mberube.Net
I turned those around. Fixed.
tvanfosson
+3  A: 

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
Andrea Celin
I think you mean writer.RenderBeginTag(HtmlTextWriterTag.P) rather than Fieldset.
Daniel Ballinger