0

I have simple literal on my page.

<asp:Literal ID="litDescription" runat="server"></asp:Literal>

When I am trying to set the following text to the literal:

<p><div>Some Text</div></p>

in my browser's markup I see

<p></p>
    <div>Some Text</div>
<p></p>

Why there are two 'p' tags? I need to have

<p><div>Some Text</div></p>.
Sanya530
  • 1,209
  • 4
  • 18
  • 24

3 Answers3

9

A div inside a P is invalid HTML. I don't think .net is making the

up. I think it comes from your browser.
Overwatch
  • 27
  • 1
  • 7
Predders
  • 375
  • 3
  • 12
  • 1
    +1. If you do a `View Source` you will see there are not the extra `p` tags, but you will in your "Inspector" (Chrome, FF, IE, etc). The browser is trying to fix the DOM to deal with an invalid situation. – BLSully Dec 04 '12 at 15:53
1

It's bad syntax to have a <div> tag inside of a <p> tag. It should be <div><p>Some Text</p></div>

in C#:

litDescription.Text = "<div>Some Text</div>";

However that is still bad syntax. If you are using the div to apply inline styling to a part of the <p> tag, use <span> instead of <div>.

litDescription.Text = "Some Text"; is the correct html syntax.

Tom
  • 1,275
  • 1
  • 18
  • 51
  • the literal control does not wrap its contents in `

    ` tags.

    – Graham Clark Dec 04 '12 at 15:48
  • My mistake. Fixed it so you can remove the -1. – Tom Dec 04 '12 at 15:49
  • Doing `litDescription.Text = "Some Text";` will just write `Some Text` on the page, with no tags. The easiest way to achieve some text inside a `span` is to use a `Label` control without setting the `AssociatedControlId` property. – Graham Clark Dec 04 '12 at 15:51
  • This was assuming he was using incorrect syntax for a reason, such as `

    Some Text and some more text

    `.
    – Tom Dec 04 '12 at 15:53
0

A different approach that is more in line with the server side control structure of asp.net web forms.

<p><div>
<asp:Literal id=DescriptionLiteral runat="server" />
</div></p>

// c# code:
DescriptionLiteral.Text = "Some Text";

Note that a <div> tag nested within a <p> tag is not valid HTML markup.

andleer
  • 22,388
  • 8
  • 62
  • 82