tags:

views:

67

answers:

2

As you may already know, when the Enter key is pressed within an ASP.NET Web Form that has multiple single-line textboxes, the form will submit and the Button Web control that appears first in the HTML page's markup will have its Click event fired.
For eg, I set focus on a textbox and press "Enter", the form submit automatically and the first Button Web Control is clicked.

How can I prevent this behaviour??
I do NOT want the first Button's click event being triggered.

Edit:
I do NOT want to disable Enter keyPress on the Form.
I just want to prevent this odd behaviour.(at least odd and unnecessary to me).
If I set focus on the textbox and press enter, why was the first button's click button triggered??

A: 

You can do it using js. Check out this website: http://www.webcheatsheet.com/javascript/disable_enter_key.php

Gmoliv
A: 
<body OnKeyPress="return disableEnterKeyPress(event)">

Here's the javascript function:

<script language="JavaScript">
function disableEnterKeyPress(e)  
{  
 var key;  
 if(window.event)  
      key = window.event.keyCode; //IE  
 else  
      key = e.which; //firefox       

 return (key != 13);  
}
</script>

Update:

I just want to prevent this odd behaviour.(at least odd and unnecessary to me). If I set focus on the textbox and press enter, why was the first button's click button triggered??

This is a bug. At least many people consider it a bug. take a look at the following post:
http://forums.asp.net/t/985791.aspx

Kamyar
I think this won't work if a current input is focused, unless he defines the "onkeypress" method inside every input.
Gmoliv
Just tested the code. Added a couple of textboxes. tested it when the textboxes are focused and vice versa. It works as expected.
Kamyar
Thanks, Kamyar..It helps me a lot..
Kai
@Kai, No problem. Glad I could help.
Kamyar