Handling key press on a web page

Posted on 2004-11-22
Last Modified: 2010-04-15
What is the best way to handle a key press (namely 'Enter') on an aspx web page, with C# code-behind.  Do I have to use javascript, and if so, how to I get that javascript to call a C# function that I have.

Basically, when I have a textbox on a page, and I hit enter, I want to initiate a search without clicking any buttons.
Question by:WhatupE
    LVL 8

    Accepted Solution

    Modify the body tag like this:

    <BODY onkeydown="if(event.keyCode == 13) {
    event.returnValue = false;
    event.cancel = true;
    document.getElementById('myBtn').click(); } ">

    This will call the 'click' event for the button 'myBtn'. So just rename that to the button on your site that will normally do the search.


    LVL 1

    Expert Comment

    Hi Whatupe,

    If your just using a standard asp web form...
    then pressing <ENTER> should automatically "press" the first button on the form.

    If for aesthetic reasons, you'd prefer not to have the search button visible, then try setting the


    properties to achieve the desired affect of an "invisible" button thats on the page but looks hidden. So all the page user will
    see is the textbox. Then they press enter and it will press the invisible button. Put your "search" code behind the button.

    Author Comment

    No, my focus is going to be in the textbox most likely, so I don't think just pressing Enter will cause the first button to be "pressed".

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now