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:
<label>
<span>Username</span>
<input type="text" id="Username" runat="server" />
</label>
What is the proper approach?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…