37

I get this as pure HTML:

<label for="txtPais">Pais:</label>    
<input name="ctl00$ContentPlaceHolder1$txtPais" type="text" id="ctl00_ContentPlaceHolder1_txtPais" class="textInput" />

In my actual code in Visual Studio I have this:

<label for="txtPais">Pais:</label>    
<asp:TextBox ID="txtPais" runat="server" CssClass="textInput"></asp:TextBox>

How would I apply a label for this textbox?

2 Answers2

78

You should use the <asp:Label...> as detailed in this blog post on Haacked

<asp:Label id="label" AssociatedControlId="txtPais" Text="Pais:" runat="server" />
<asp:TextBox id="txtPais" runat="server" CssClass="textInput" />

This should convert correctly with the ID being converted.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
-1

It is recommended to wrap some inputs inside of labels for accessibility (See this example).

<asp:Label ID="UsernameLabel"
           Text="Username:"
           AssociatedControlID="UsernameTextBox"
           runat="server">
    <asp:TextBox ID="UsernameTextBox" runat="server" />
</asp:Label>

I got this answer from the post that, as it happens is mentioned in a comment in the original question.

Kevin Scharnhorst
  • 721
  • 1
  • 7
  • 14