2

I have a label element as below:

<asp:Label ID="date" runat="server" Text="<%$ Resources:Resource, DATE%>" CssClass="col-sm-4 control-label" />

When I render this in the browser somehow this gets generated to span element as below:

<span id="ctl00_MainContent_FormDate_Date" class="col-sm-4 control-label">Date</span>

which makes it look different on the page.

But when I use text instead of resource:

<asp:Label ID="date" runat="server" Text="Date" CssClass="col-sm-4 control-label" />

this renders correctly:

<label for="ctl00_MainContent_FormDate_Date" class="col-xs-4 control-label">Date</label>

Anyone come a cross this issue and how can I fix the issue to render as <label> when using resources?

akd
  • 6,538
  • 16
  • 70
  • 112
  • 6
    The ASP.NET `Label` always rendered as a span. Only if you assign a [`AssociatedControlID`](https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.label.associatedcontrolid(v=vs.110).aspx) it will be rendered as a label. – Tim Schmelter Jan 07 '16 at 14:48
  • That's not true. When I use simple text it gets rendered as label – akd Jan 07 '16 at 14:50
  • 1
    You can use an html label control with runat=server. – Abdul Rehman Sayed Jan 07 '16 at 14:51
  • @akd: if i use `` it gets rendered as span. Btw, why do you want a label which is associated to itself? – Tim Schmelter Jan 07 '16 at 14:55
  • I want it to be rendered as label instead of span. Because some of the form elements gets rendered as label and some span. label elements look bold and span element look unbold. which does not look good on the UI. – akd Jan 07 '16 at 15:01
  • I tried to use label Abdul but this time do you know how I would add the resource as label text? this doesnt work. – akd Jan 07 '16 at 15:02
  • Doesn't work because you get an error? Also, it seems to be a CSS issue if only label controls get the correct style. – Tim Schmelter Jan 07 '16 at 15:09
  • Thanks Tim. I sorted out by using AssociatedControlID attribute. when there is AssociatedControlID it gets rendered as label. Otherwise it gets rendered as span. – akd Jan 07 '16 at 15:23
  • 1
    Possible duplicate of [How to use the – TylerH Apr 23 '19 at 16:57

1 Answers1

4

Though I know you have already got the solution, I am answering for the future reader of the post.

If you set the Label.AssociatedControlID value it becomes a label element. If it is not associated to a control or in other words not being used as a label element it becomes a span element.

A label which is not associated with a control is considered as a bad mark up. Hence if the label control is not assigned to a control, instead of placing a label .NET places that as a span element.

Mamun
  • 66,969
  • 9
  • 47
  • 59