?
Solved

Handling key press on a web page

Posted on 2004-11-22
3
Medium Priority
?
272 Views
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.
0
Comment
Question by:WhatupE
3 Comments
 
LVL 8

Accepted Solution

by:
Razzie_ earned 500 total points
ID: 12646661
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.

HTH,

Razzie
0
 
LVL 1

Expert Comment

by:cambo1982
ID: 12646788
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

Button.ForeColor
Button.BackColor
Button.BorderColor
Button.BorderStyle
Button.Text

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.
0
 

Author Comment

by:WhatupE
ID: 12646837
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".
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

862 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