Udostępnij za pośrednictwem


How to: Set Access Keys for ASP.NET Web Server Controls

An access key (also known as a hot key) allows users to press the ALT key plus another key to jump to a specific control on the page without using the mouse.

You can set an access key on a specific control, such as a TextBox or ListBox control. Alternatively, you can set an access key for a Label control, and then specify that the Label control is associated with another control. This approach enables you to use the Label control as a caption, allowing you to indicate the access key with an underlined letter in the label text.

NoteNote

Access keys and other keyboard shortcuts are not supported on all browsers.

To set an access key for a specific ASP.NET Web server control

  • Set the control's AccessKey property to the letter or number that you want to use with ALT key as a keyboard shortcut.

    For example, to set a control's access key to ALT+S, set the control's AccessKey property to S.

    NoteNote

    Some ALT-key sequences might be reserved in the browser. For example, in Internet Explorer, ALT+F opens the File menu. The browser determines whether to give precedence to your ALT-key sequence or to the brower's reserved keys.

To set an access key by using a Label control

  1. Add a Label control to the page to act as a caption for the control that you want to put focus on with an access key.

  2. In a Label control, set the following properties:

    • AccessKey    Set this property to the letter or number to use with the ALT key, such as S to support ALT+S.

    • AssociatedControlID   Set this property to the ID of the control to set the focus on when the ALT-key sequence is pressed.

    • Text   Optionally, use HTML to add an underline or other indicator to the Text property.

    The following code example shows the markup for a Label control and a TextBox control. The Label control displays a caption for the TextBox control, with the letter L underlined to indicate that the access key for the text box is ALT+L.

    Security noteSecurity Note

    This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

    <asp:Label ID="Label1" runat="server" 
      AccessKey="L" AssociatedControlID="TextBox1"  Text="<u>L</u>ast name: ">
    </asp:Label>&nbsp;
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    
    NoteNote

    Setting focus by using an access key from a Label control requires that client scripting is enabled in the browser.

See Also

Concepts

Tab Order, Focus, and Access Keys in ASP.NET Web Server Controls