13

Simple question, difficult to search for (due to the main part of the question being the single letter p)!

In ASP.NET, <asp:Panel/> is rendered as a <div></div> block, <asp:Label/> is rendered as a <span></span> block... is there one that renders as <p></p> block?

It doesn't look like it from MSDN for the WebControl class, but I thought I would ask in case I've missed something obvious.

(I realise the obvious solution is to just use <p runat="server" id="p1"></p> and use the generic html control class)

freefaller
  • 19,368
  • 7
  • 57
  • 87

3 Answers3

17

No, there is no built-in control specifically for <p>. A LiteralControl or the <p runat="server" /> version you gave are the closest you will get.

You could always create your own control, though. You can create a class that implements WebControl, and override the Render method:

protected override void Render(HtmlTextWriter output)
{
   output.WriteFullBeginTag("p");
   output.Write(this.Text);
   output.WriteEndTag("p");
}

There are more instructions on how to write your own server controls here:

And a list of all of the .NET web and server controls here:

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
  • Thanks Cory, that is as I expected... I was hoping for a `doh` moment as somebody pointed me at the all-too-obvious class that I'd missed. Once I get my 6 months off to do whatever work I want to do myself, I'll get around to creating one `` ;-) – freefaller Oct 25 '13 at 12:20
  • The thing is that if you override the Render() method, you will have to take care of all of the rendering yourself. If you do it using the approach I mentioned here, the control will render just as you would expect from a Label, only with the p tag instead. – aahoogendoorn Oct 18 '14 at 07:48
6

I've had exactly the same issue. After I started to use a similar technique Cory uses here, I found an even easier one, that basically does the same and solves your issues. However, it has one benefit when compared with the solution above: you don't have to render the whole control yourself.

Basically all you need to do is the following:

  • Create you own control, inherit e.g. from the Label control.
  • Override the RenderBeginTag() method.
  • Write your own tag, e.g. p.
  • The end tag will be written automatically.
  • Your new tag will now replace the default tag (span in this example).

See code below:

    public class P : Label
    {
        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            writer.RenderBeginTag("p");
        }
    }
aahoogendoorn
  • 444
  • 6
  • 9
  • 1
    I have added this line at the top of the method, to include the CssClass attribute: `writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass);` – Christian Davén Nov 10 '14 at 15:19
  • Instead of overriding the `RenderBeginTag()` method, you can also override the `TagKey` property: `protected override HtmlTextWriterTag TagKey => HtmlTextWriterTag.P;` – chrisofspades Apr 13 '22 at 15:50
0

Although there is no special class for <p> element, you can use HtmlGenericControl to generate <p> tag like this:

var p = new HtmlGenericControl("p");
Jalal
  • 6,594
  • 9
  • 63
  • 100