In this article we will explore the use of AccessKey and AssociatedControlId in the label control. If we are not using above both feature, I would say we are not leveraging the useful feature of asp.net label control.
Notes:
1. Access keys are not supported by all the browser. 2. Some ALT key sequence might be reserved by browser. Browser ALT key sequence gets the precedence over user defined. 3. Client scripting needs to be enabled to set focus by using access key of label.
Let's evaluate AssociatedControlId and AccessKey with different controls.
TextBox
On click of Name label or on press of ALT+N focus will be on name textbox.
<asp:Label ID="lblName" Text="<U>N</U>ame" runat="server" AccessKey="N" AssociatedControlID="txtName"></asp:Label> <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
HTML output of the above will be

DropDownList On click of Country label or on press of ALT+C focus will be on country dropdownlist.
<asp:Label ID="lblCountry" Text="<U>C</U>ountry" runat="server" AccessKey="C" AssociatedControlID="ddlCountry"></asp:Label> <asp:DropDownList ID="ddlCountry" runat="server"> <asp:ListItem Text="India" Value="1"></asp:ListItem> <asp:ListItem Text="USA" Value="2"></asp:ListItem> </asp:DropDownList>
HTML output of the above will be
LinkButton
On press of ALT+M, access key in the link button opens up the link in few browsers and in others it sets the focus on the link.
<asp:LinkButton AccessKey="M" PostBackUrl="http://www.microsoft.com" Text="<u>M</u>icrosoft" ID="lnl" runat="server"></asp:LinkButton>
HTML output of the above will be

This ends the article of access key and associated controlid artilce.
Live Demo
|