How to use the <label> tag in ASP.NET?
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"
Text="Username:"
AssociatedControlID="UsernameTextBox"
runat="server">
<asp:TextBox ID="UsernameTextBox" runat="server" />
</asp:Label>
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