Handling key press on a web page

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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
WhatupEAuthor Commented:
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".
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.