Using Access Key or Hot Key in ASP.NET

This article explain you how to set a access key or hot key on ASP.NET controls to navigate a specific control on the page without using mouse.
  • 4849

An access key or a hot key allows you to press the ALT+ another specified key to navigate a specific control on the page without help of mouse. User can set an access key on a specified control like textbox, listbox or a label control.
This article explain how to set a access key on a Label control in
ASP.NET. When the page renders, user will be able to pres ALT+ the
specific key defined for each label.

 

How to set an access key by using a Label control

  1. Add a label or other control on which you want to set the access key.
  2. In the label control, set the AccessKey to letter or number to use with ALT key, then set the AssociatedControlID to the ID of the control to set the focus on when the ALT-key sequence is pressed and optionally highlight the
    text as underline it or use other indicator to the text property.

The following example shows the markup for a label control and a textbox control with the letter N,A,Q and J underlined to indicate that the access key for the text
box.

access-key.gif
 

When you press ALT+ any access key defined for each control on the page, you can directly navigate that control without using the mouse. Lets see the code snippets:  

Code Snippets

<%@ Page Language="vb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
   <title>ASP.NET Label HotKeys</title>
</head>
<
body>
   <form id="form1" runat="server">
   <div id="container">
   <h3>AccessKey for ASP.NET Label Control</h3>
   <asp:Label ID="labName" runat="server"
      AccessKey="N" AssociatedControlID="txtName" Text="Full <u>N</u>ame"></asp:Label>
   <asp:TextBox ID="txtName" runat="server"></asp:TextBox><br /><br />
   <asp:Label ID="LabelAdd" runat="server"
      AccessKey="A" AssociatedControlID="txtAddress" Text="<u>A</u>ddress"></asp:Label>
   <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox><br /><br />
   <asp:Label ID="LabelQual" runat="server"
          AccessKey="Q" AssociatedControlID="txtQual" Text="<u>Q</u>ualification"></asp:Label>
   <asp:DropDownList ID="txtQual" runat="server">
         <asp:ListItem>UnderGraduate</asp:ListItem>
         <asp:ListItem>Graduate</asp:ListItem>
         <asp:ListItem>PostGraduate</asp:ListItem>
   </asp:DropDownList><br /><br />
   <asp:Label ID="labMonth" runat="server"
          AccessKey="J" AssociatedControlID="drpMonth" Text="Month of <U>J</U>oining"></asp:Label>  
   <
asp:DropDownList ID="drpMonth" runat="server">
         <asp:ListItem>January</asp:ListItem>
         <asp:ListItem>February</asp:ListItem>
         <asp:ListItem>April</asp:ListItem>
         <asp:ListItem>May</asp:ListItem>
         <asp:ListItem>June</asp:ListItem>
         <asp:ListItem>July</asp:ListItem>
         <asp:ListItem>August</asp:ListItem>
         <asp:ListItem>September</asp:ListItem>
         <asp:ListItem>October</asp:ListItem>
         <asp:ListItem>November</asp:ListItem>
         <asp:ListItem>December</asp:ListItem>
   </asp:DropDownList>
   </div>
   </form>
</body>
</html>

I hope this article will help you to understand how to use access key or hot key in ASP.NET.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.