Welcome to DevAuthority.Com Sign in | Join | Help

The AssociatedControlID property of the Label web control

This blog has moved to DotNetSlackers, please update your links and post comments there.

In ASP.NET 2.0 the Label web control exposes a new property, AssociatedControlID, which takes the ID of another control in the page, that is, a TextBox or another input control.
When the property is set, the rendering of the Label switches from a simple <span> tag to a <label> tag, whose for attribute assumes the client-side ID of the associated control as the value.

A sample to clear things up

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

becomes:

<span id="Label1">Label</span>
<
input name="TextBox1" id="TextBox1" type="Text" />

While

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

becomes:

<label for="TextBox1" id="Label1">Label</label>
<
input name="TextBox1" id="TextBox1" type="Text" />

The difference

The difference is an accessibility feature. Setting the AssociatedControlID property any click on the Label extends to the associated control. In case of an associated TextBox the effect is that when clicking on the Label the TextBox gains focus.

Published Wednesday, May 17, 2006 9:53 AM by SimoneB
Filed Under:

Comments

# accesibilidad y shortcuts en ASP.NET 2.0

accesibilidad es todo un tema, voy a dejar abajo&amp;nbsp;varios links como referencia. En este post, lo...
Monday, June 18, 2007 8:40 PM by Sergio Tarrillo's Blog -> enhancements

# accesibilidad y shortcuts en ASP.NET 2.0

accesibilidad es todo un tema, voy a dejar abajo varios links como referencia. En este post, lo vamos
Thursday, August 09, 2007 10:49 PM by SergioTarrillo's RichWeblog
New Comments to this post are disabled