AssociatedControlID Property in ASP.NET

I can see many websites today having no such feature like when user clicks on label control textbox gets focus. As you can see in animated image given below, in first login set when I clicked on label control text box not getting focus and the same in another login set textbox gets focus and we can start typing.

Reason: Such websites not using AssociatedControlID property.

Use of AssociatedControlID property

We use AssociatedControlID property to associate a Label control with another server control on a Web form. When a Label control is associated with another server control, its attributes can be used to extend the functionality of the associated control. You can use the Label control as a caption for another control, or you can set the tab index or hot key for an associated control.
When the AssociatedControlID property is set, the Label control renders as an HTML label element, with the for attribute set to the ID property of the associated control. You can set other attributes of the label element using the Label properties. For example, you can use the Text and AccessKey properties to provide the caption and hot key for an associated control.

If you are an ASP.NET guy, you can use:

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

If you are an HTML guy, you can use:

<label for="UserName" id="UserNameLabel">Username:</label>
<input name="UserName" type="text" id="UserName" />

Means, in ASP.NET we use AssociatedControlID property and in HTML we use 'for' attribute for the same result.


Popular posts from this blog

Migrating database from ASP.NET Identity to ASP.NET Core Identity

Customize User's Profile in ASP.NET Identity System

Lambda two tables and three tables inner join code samples