views:

29

answers:

2

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?

+6  A: 

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
+1 Properly the best safe and clean way.
lasseespeholt
+1  A: 
<label for="<%= txtPais.ClientID %>">Pais:</label>    
<asp:TextBox ID="txtPais" runat="server" CssClass="textInput"></asp:TextBox>

Or the following (Asp.net 4). But ensure you don't get a name clash. This way the TextBox is called the specified name in the output.

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

Or use the Asp.Net asp:Label as suggested in another answer.

lasseespeholt