1

I'm currently looking through some of my "dynamically generated" HTML and noticed this:

<div id="MainContent2_news"><span id="MainContent2_ctl00_newsLabel"><...

My C# looks like this: <asp:Label ID="newsLabel" runat="server" />

Is there any way to change the SPAN to a DIV? I'm getting some ugly XHTML Validation errors.

TylerH
  • 20,799
  • 66
  • 75
  • 101
michaelmcgurk
  • 6,367
  • 23
  • 94
  • 190
  • 4
    yes, switch to asp.net mvc #scnr . no, really, if you need control over your html, webforms isn't your friend – flq May 26 '11 at 20:41
  • its really not webforms, controls are a feature of asp.net and they are available in webforms and mvc :) – Allen Rice May 26 '11 at 20:53

3 Answers3

2

Almost each (or I believe every) ASP.NET control has its correpondent HTML tag, which is sent this way so every browser can interpret. The DIV tag is rendered by asp:Panel.

If you are having problem with the SPAN tag, you can try the asp:Literal, which doesn't append any HTML tag to your rendered control.

Tuco
  • 1,620
  • 1
  • 14
  • 15
1
<div id="newsLabel" runat="server"></div>

To interact with it via postbacks or page_load, do this

this.newsLabel.InnerText = "content";

BTW, the problem with the markup is with the control which is a feature of asp.net. Both webforms and mvc.net run on top of asp.net, so switching between webforms and mvc.net isn't going to help you.

Also, if you're interested, the above code will generate a control of type System.Web.UI.HtmlControls.HtmlGenericControl

Allen Rice
  • 19,068
  • 14
  • 83
  • 115
1

A span is generated because it's an inline asp element.

If you want elements which do not generate any tag, use Literal or PlaceHolder.

If you want to have a div-tag, use you can use the Panel or just do this <div class="someclass" runat="server" id="myDiv">Text</div>

jerone
  • 16,206
  • 4
  • 39
  • 57