The AssociatedControlID property of the Label web control

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.

An example 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 03 July 2006 09:14 PM by simoneb
Filed under:

Comments

No Comments

This site

Search

Go

This Blog

News

Syndication

Sponsors

  • MaximumASP
  • Social Bookmarking
    Online Shopping
    asp.net hosting
    UK online local dating