2

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!

3 Answers 3

8

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...

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

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" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.