473,398 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Capture Enter Key in ASP.NET

Hi,

ASP.NET has some interesting behavior when the user hits the Enter key.
If there are multiple ASP:Buttons (rendered as HTML submits) on the
form, when the user hits enter, the first button's click event will
fire and the page will submit.

I have a series of pages with Previous and Next navigational Btns. The
Previous button is the first button, so when the user hits enter, the
previous page is served up. Enter should result in the NEXT page being
served up.

I would like to capture the Enter keystroke, suppress the Previous Btn
click event and call a method to trigger the Next Btn click event, thus
submitting the page correctly.

That's putting it simply. My implementation is slightly more
complicated. The Previous and Next Btns are on a user control (so they
can be used on each page). Also, on some pages, there are more buttons
and different scenarios in which an Enter Click should not result in
the page submitting at all. I basically need to capture the Enter
click so that I may then determine where the user's focus is and then
call the right handlers.

I'm pretty sure JavaScript will be needed in this solution.

Thanks!
Eric

Nov 19 '05 #1
5 3108
This is not a .NET issue, it is a function of the client. You'll need to
add a client-side script that captures the onKeyPress event and checks for
the ENTER key.


<ew*****@gmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
Hi,

ASP.NET has some interesting behavior when the user hits the Enter key.
If there are multiple ASP:Buttons (rendered as HTML submits) on the
form, when the user hits enter, the first button's click event will
fire and the page will submit.

I have a series of pages with Previous and Next navigational Btns. The
Previous button is the first button, so when the user hits enter, the
previous page is served up. Enter should result in the NEXT page being
served up.

I would like to capture the Enter keystroke, suppress the Previous Btn
click event and call a method to trigger the Next Btn click event, thus
submitting the page correctly.

That's putting it simply. My implementation is slightly more
complicated. The Previous and Next Btns are on a user control (so they
can be used on each page). Also, on some pages, there are more buttons
and different scenarios in which an Enter Click should not result in
the page submitting at all. I basically need to capture the Enter
click so that I may then determine where the user's focus is and then
call the right handlers.

I'm pretty sure JavaScript will be needed in this solution.

Thanks!
Eric

Nov 19 '05 #2
Hi,

You'll need to use Javascript. In your body tag place this:

onkeydown="CheckKey(event);"

Then do what you need to do in the CheckKey() function:

function CheckKey() {
if (event.keyCode == 13) {
document.getElementById("btnLogin").focus();
}
}

The function above tests for the Enter key. If the Enter key is pressed it
switches focus to the btnLogin button which causes the client to apply the
Enter key as if the btnLogin button had the focus to begin with. In this
case the button just does a post back. Note you could also capture the 'N'
and 'P' keys so the user could hit those to navigate. I normally capture
the Alt key and then display hot-key codes on all my buttons so users can
hit 'Alt-S' to save, 'Alt-N' for next and etc. This let's my clients use
the web app just like they would a data entry intensive Windows or Terminal
app. Good luck! Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

<ew*****@gmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
Hi,

ASP.NET has some interesting behavior when the user hits the Enter key.
If there are multiple ASP:Buttons (rendered as HTML submits) on the
form, when the user hits enter, the first button's click event will
fire and the page will submit.

I have a series of pages with Previous and Next navigational Btns. The
Previous button is the first button, so when the user hits enter, the
previous page is served up. Enter should result in the NEXT page being
served up.

I would like to capture the Enter keystroke, suppress the Previous Btn
click event and call a method to trigger the Next Btn click event, thus
submitting the page correctly.

That's putting it simply. My implementation is slightly more
complicated. The Previous and Next Btns are on a user control (so they
can be used on each page). Also, on some pages, there are more buttons
and different scenarios in which an Enter Click should not result in
the page submitting at all. I basically need to capture the Enter
click so that I may then determine where the user's focus is and then
call the right handlers.

I'm pretty sure JavaScript will be needed in this solution.

Thanks!
Eric

Nov 19 '05 #3
Thanks for the info, Ken.

I'm capturing the Entery Key stroke successfully, but I can't manage to
suppress the btn click event that is ocurring on the wrong (Previous)
btn.

Here's my js:
<script language=javascript>
function captureEnterKey() {
if(event.keyCode == 13) {
alert('keycode= ' + event.keyCode);
(document.getElementById(NavStrip1.CmdNextID)).foc us();
}
}
</script>

Where NavStrip1 is my user control containing the btns. It's registered
on page like so:

<%@ Register TagPrefix="uc1" TagName="NavStrip"
Src="Controls/Wizard/NavStrip.ascx" %>

And included like so:
<uc1:navstrip id=NavStrip1 runat="server"></uc1:navstrip>

CmdNextID is a property exposing the name of the btn that I want to
fire. Like so:

public string CmdNextID {
get { return "cmdNext"; }
}

Here's the js call:

<body MS_POSITIONING="GridLayout" onkeydown="javascript:
captureEnterKey();">

The alert is being reached, but a breakpoint on the CmdNextID property
never gets hit. And, most importantly, cmdPrevious btn is firing.

Thanks,
Eric

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 19 '05 #4
changed JS to:
<script language=javascript>
function captureEnterKey() {
if(event.keyCode == 13) {
alert('keycode= ' + event.keyCode);
(document.getElementById(NavStrip1.CmdNextID)).foc us();
event.returnValue = false;
}
}
</script>

SAME RESULT. Wrong btn fires.

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 19 '05 #5
Hi,

Try commenting out the alert() line and see if does anything. Also, keep
the alert() line commented out and put a breakpoint (debugger;) just before
this line:

(document.getElementById(NavStrip1.CmdNextID)).foc us();

Find out if document.getElementById(NavStrip1.CmdNextID) equals what it
should.

One other thing is to change this line:

(document.getElementById(NavStrip1.CmdNextID)).foc us();

To this:

document.getElementById(NavStrip1.CmdNextID).focus ();

Don't know if it will make a difference but I don't think you need the extra
set of paranthesis. If that still doesn't work then there is a good chance
that the tab strip you are using is doing the same thing you are doing now
and overriding the code you've added. Check if there is a property on it to
change which button submits when Enter is pressed. Good luck! Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"ewillyb" <ew*****@gmail.com> wrote in message
news:Od**************@TK2MSFTNGP14.phx.gbl...
changed JS to:
<script language=javascript>
function captureEnterKey() {
if(event.keyCode == 13) {
alert('keycode= ' + event.keyCode);
(document.getElementById(NavStrip1.CmdNextID)).foc us();
event.returnValue = false;
}
}
</script>

SAME RESULT. Wrong btn fires.

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com

Nov 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: PA | last post by:
Hi, Can anyone tell me how to capture events of iframes like keypress. I have an editable iframe. When I hit <enter> it actually prints <p> instead of <br>. (innerHTML) Does anyone know how...
1
by: Matthew Wieder | last post by:
Hi - I wanted to capture the enter button on a form since I have a datagrid with the first column being a delete button and if someone hits enter it deletes the first record. I coded: private...
2
by: marcia | last post by:
How can I capture (i.e., write an event handler) that runs after I press the "Enter" key? I'd like to prevent a Web Form from being submitted when the "Enter" key is pressed following the typing...
7
by: Bob Achgill | last post by:
When I use the code for KeyPress to capture pressing a certain key for processing on a form with no Text Box it works. But when I try the same code on my application that has text boxes it does...
3
by: Melson | last post by:
hi can anyone help me how can i capture ENTER keystroke when the cell in datagrid is in editing mode. I'm now creating a data entry form with primary key in header and details in datagrid. So...
4
by: sck10 | last post by:
Hello, I want to allow all users on our company intranet (domain\handle) to enter our website. However, I would like to capture the handle of the person entering my website. I was told that if...
3
by: GS | last post by:
If I were to capture enter key on keyup event of an GUI object like comboBox box, How do I avoid killing ctrol-c, controlv, controlx, shift-insert, shiftdelete, contrl-insert
1
xarzu
by: xarzu | last post by:
How do I capture the event in a CEdit control when the user presses enter or return in a dialog box? I have so far figured out how to disable the event when the user hits return which normally would...
5
by: sourav08 | last post by:
Hello, I'm developing a website using PHP, Javascript and MySQL. I have a login form with a couple of textboxes and a login button. What I want to do is capture the Enter key so that when the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.