How to use label for in an ASP.Net webform?

delete picture delete · Aug 16, 2010 · Viewed 34k times · Source

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?

Answer

ChrisF picture ChrisF · Aug 16, 2010

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.