Home » CodeDigest


AssociatedControlID property in ASP.Net Label Control
Submitted By Satheesh Babu B
On 11/24/2008 6:58:52 AM



In web application, we mostly use label control to specify a heading to an input control. The AssociatedControlID property of the Label will take an input control ID as its value. Hence, the Label is associated with the input control. Association means, when the page is executed and if we click on the Label Text the focus will be returned to the associated input control.


 Consider, we have a form to get input from users. Every input control like textbox should have a heading text. For example,


<asp:Label ID="Label1" runat="server" Text="Enter your Name" AssociatedControlID="txtName"></asp:Label>

            <asp:TextBox ID="txtName" runat="server"></asp:TextBox>


In the above code, Label1 is associated to the TextBox "txtName". When executed and the focus will be returned to the TextBox if we click on the label text "Enter your Name".


A Label control without AssociatedControlID will be rendered differently when compared to Label control with AssociatedControlID.


The above ASP.Net markup will be rendered as,


<label for="txtName" id="Label1">Enter your Name</label>

            <input name="txtName" type="text" id="txtName" />


The "for" property in HTML label will return the focus to the associated textbox.

But a Label control without AssociatedControlID will be rendered as <span> tag.



Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..