How can I use the tag within an ASP.NET application? I want it to be valid, accessible, and usable.
I understand the optimal HTML way is t
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>
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>
<p><asp:Label ID="label1" Text="Username:" AssociatedControlID="txtUserName" runat="server"> <asp:TextBox ID="txtUserName" runat="server" /></asp:Label></p>
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" />
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.
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