views:

136

answers:

3

Hi guys,

I really like the HTML tag which makes it really easy to see which label stands for which input element by checking its 'for' attribute. You know, something like:

 <label for="txtInput"> Enter your name</label>
 <input type="text" id="txtInput" />

Is it possible to do something similar in asp.net Label control so that I can see it stands for what input control? I could not see an attribute for that. Without extending the control?

Thanks!

+5  A: 

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.label.associatedcontrolid.aspx

<asp:label AssociatedControlID="textbox1" runat="server" id="lblOne" />
<asp:textbox id="textbox1" runat="server" />

Not tested but along those lines...

RichardOD
a few capitalization issues...but still a good answer
Chris Shouts
A: 

If you set the AssociatedControlID property of the <asp:Label> control it will write out an HTML <label> instead of a <span>

Martin Brown
A: 

AssociatedControlID works in .net 2+ in earlier versions you need to do something like the following:

<label for="<%=textbox1.ClientID %>">label text</label><asp:textbox id="textbox1" runat="server" />
James