How to use the tag in ASP.NET?

How can I use the <label> tag within an ASP.NET application? I want it to be valid, accessible, and usable.

I understand the optimal HTML way is this:

<label for="Username">Username:</label>
<input type="text" id="Username" runat="server" />

But if the above code is in an ASP.NET user control, the input ID will change, meaning the label’s “for” attribute is useless. I could make the label tag a server control and set its “for” attribute in the code (Username.ClientID), but it seems like a lot of work for such a simple thing.

I’ve also seen this HTML used in the past:

    <input type="text" id="Username" runat="server" />

What is the proper approach?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I use <asp:Label ... AssociatedControlID="Username" ...> controls for this. They get rendered as <label> tags and set the for attribute appropriately.

Note that you can also nest other tags within the Label control if you wish:

<asp:Label ID="UsernameLabel"
    <asp:TextBox ID="UsernameTextBox" runat="server" />

Method 2

You can also write it like this:

<label for="<%= Username.ClientID %>">Username:</label>
<asp:TextBox ID="Username" runat="server" />

Phil Haack has a blog post on this topic

Method 3

use the <asp:Label> server control. It has a property that you can use to set the associated control ID.

<asp:Label ID="label1" runat="server" Text="Username" AssociatedControlID="Text1" />
<asp:TextBox ID="Text1" runat="server" />

Method 4

I guess the easiest way to do it is this.

<asp:Label AssociatedControlID="Username" runat="server" Text="Username:"></asp:Label>
<asp:TextBox ID="Username" runat="server"></asp:TextBox>

Method 5

If you want a label, but don’t have another control to use in AssociatedControlID one can use the label itself

<asp:Label ID="Not_Span" AssociatedControlID="Not_Span" Text="Will be rendered as label" />

Method 6

If you are using .NET 4 you can now use the ClientIDMode property to configure one or more controls to use static or predictable ID’s. The ClientIDMode property can be set on the TextBox directly or you can set it on any parent control or the containing page.

<label for="Username">Username:</label>
<asp:TextBox ID="Username" runat="server" ClientIDMode="Static" />

Read more about the ClientIDMode on MSDN

Method 7

<p><asp:Label ID="label1"           Text="Username:"           AssociatedControlID="txtUserName"           runat="server">    <asp:TextBox ID="txtUserName" runat="server" /></asp:Label></p>

Method 8

You my also try and this:

<asp:Label  ID="Label1" runat="server" Text="label"></asp:Label>

This is what Visual Studio, or any other software gives you if you drag and drop a label.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x