Etiqueta Html etiqueta y ASP.NET


¿Cuál es la mejor manera de usar la etiqueta <label> dentro de un ASP.NET ¿solicitud? Quiero que sea XHTML válido, accesible y utilizable.

Entiendo que la manera óptima es esta:

<label for="Username">Username:</label>
<input type="text" id="Username" runat="server" />

Pero si el código anterior está en un ASP.NET control de usuario, el ID de entrada cambiará, lo que significa que el atributo "for" de la etiqueta es inútil. Podría hacer de la etiqueta un control de servidor y establecer su atributo" for " en el código (Nombre de usuario.ClientID) pero parece un montón de trabajo para un simple cosa.

También he visto este HTML usado en el pasado:

<label>
    <span>Username</span>
    <input type="text" id="Username" runat="server" />
</label>

¿Cuál es el enfoque óptimo?

Author: Alex York, 2009-01-30

8 answers

Utilizo <asp:Label ... AssociatedControlID="Username" ...> controles para esto. Se representan como etiquetas <label> y establecen el atributo for apropiadamente.

Tenga en cuenta que también puede anidar otras etiquetas dentro del control de etiquetas si lo desea:

<asp:Label ID="UsernameLabel"
           Text="Username:"
           AssociatedControlID="UsernameTextBox"
           runat="server">
    <asp:TextBox ID="UsernameTextBox" runat="server" />
</asp:Label>
 65
Author: Sean Bright,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-01-29 22:54:19

También puedes escribirlo así:

<label for="<%= Username.ClientID %>">Username:</label>
<asp:TextBox ID="Username" runat="server" />

Phil Haack tiene una entrada de blog sobre este tema

 17
Author: Christian Hagelid,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-07-29 04:43:08

Utilice el control del servidor <asp:Label>. Tiene una propiedad que puede usar para establecer el ID de control asociado.

<asp:Label ID="label1" runat="server" Text="Username" AssociatedControlID="Text1" />
<asp:TextBox ID="Text1" runat="server" />
 9
Author: Matt Brunell,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-01-29 22:43:35

Supongo que la forma más fácil de hacerlo es esta.

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

EDITAR: Wow.. todas las mismas respuestas en un minuto. =)

 7
Author: Brian Kim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-01-29 22:43:44

Si está utilizando. NET 4, ahora puede usar la propiedad ClientIDMode para configurar uno o más controles para usar ID estáticos o predecibles.La propiedad ClientIDMode se puede establecer en el cuadro de texto directamente o puede configurarla en cualquier control padre o en la página contenedora.

<label for="Username">Username:</label>
<asp:TextBox ID="Username" runat="server" ClientIDMode="Static" />

Lea más sobre el ClientIDMode en MSDN

 3
Author: Christian Hagelid,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-21 14:29:38

Si desea una etiqueta, pero no tiene otro control para usar en AssociatedControlID, puede usar la etiqueta en sí

<asp:Label ID="Not_Span" AssociatedControlID="Not_Span" Text="Will be rendered as label" />
 3
Author: RMalke,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-10-13 13:45:43
<p><asp:Label ID="label1"           Text="Username:"           AssociatedControlID="txtUserName"           runat="server">    <asp:TextBox ID="txtUserName" runat="server" /></asp:Label></p>
 0
Author: chugh97,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-01-30 09:53:24

Usted mi también tratar y esto:

<asp:Label  ID="Label1" runat="server" Text="label"></asp:Label>

Esto es lo que Visual Studio, o cualquier otro software le da si arrastra y suelta una etiqueta.

 0
Author: magn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-10 12:29:18