28

I have an aspx page that postsback when it should not. there are two text boxes, two listboxes and two buttons on the page. if at any-point the enter key is pressed the first button is given focus and "clicked" resulting in a loss of selection within the listboxes.

How do I disable this? There are tons of tutorials on how to capture the enter button and execute a method but I could not find one on how to simply disable the neat "let me grab the first button I find and click it" feature mentioned above.

Dov Miller
  • 1,958
  • 5
  • 34
  • 46
George
  • 4,323
  • 3
  • 30
  • 33
  • 1
    See also http://stackoverflow.com/questions/1934223/disabling-default-button-or-enter-key-in-asp-et-c-sharp – Rory Aug 06 '12 at 20:11

6 Answers6

39
  • You could set the DefaultButton on the Form or a Panel. This way you have full control what happens.
  • Set UseSubmitBehavior="False" on your Buttons. This disables the "AutoPostback" on Enter.

I would prefer the second if i wanted to prevent Postbacks on Enter completely.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • If its an imagebutton .. whats the solution.?? – SRJ Nov 18 '14 at 07:55
  • 1
    @SRJ: you could use an `Image` instead. Or have a look at this question which addresses that issue: http://stackoverflow.com/questions/1561400/how-to-disable-submit-behaviour-of-aspimagebutton – Tim Schmelter Nov 18 '14 at 08:00
  • Using the "" and the button's attribute "UseSubmitBehavior" to false was my solution for a similar issue. Here's this bit of the source: https://pastebin.com/9hXJBrmk – Eric Milliot-Martinez Jan 24 '18 at 19:49
29

are you using jQuery?

if so:

$(document).keypress(function(e)
{
    if(e.keyCode === 13)
    {
        e.preventDefault();
        return false;
    }
});
bevacqua
  • 47,502
  • 56
  • 171
  • 285
  • 4
    Wouldn't that stop the enter key from inserting a blank line in textarea? – Tony_Henrich Jun 01 '12 at 17:44
  • It would also stop the submit behavior on enter press when the focus is in that textbox where the enter press should invoke the submit (`i.e username/password textbox`) – sohaiby Sep 15 '16 at 12:38
  • Great solution. @sohaiby @Tony_Henrich It is easy to replace `document` to a selector that targets exactly those inputs where the Enter key should be suppressed. It is also easy, however pointless, to circumvent jQuery by using plain JS. – Roland Mar 06 '18 at 14:59
9

Use the below code to disable enter key causing postback. This piece of code will block the enter key in all browsers 4.0 above, except when enter is pressed in a Textarea or on the Submit button itself.

<script language="JavaScript">
var nav = window.Event ? true : false;
if (nav) {
window.captureEvents(Event.KEYDOWN);
window.onkeydown = NetscapeEventHandler_KeyDown;
} else {
document.onkeydown = MicrosoftEventHandler_KeyDown;
}

function NetscapeEventHandler_KeyDown(e) {
if (e.which == 13 && e.target.type != 'textarea' && e.target.type != 'submit') { 
return false; 
}
return true;
}

function MicrosoftEventHandler_KeyDown() {
if (event.keyCode == 13 && event.srcElement.type != 'textarea' && 
event.srcElement.type!= 'submit')
return false;
return true;
}
</script>
Priya
  • 178
  • 1
  • 4
  • 1
    I've attempted to use so many different javascript event handlers to cancel an asp form submit on Enter, but none of them work as well as this one. I can event change the "return false;" lines to another javascript method to always have Enter submit the data I want. Thanks! – John Suit Mar 13 '18 at 19:40
3

Only put this on you ASP.Net TextBox control:

<asp:TextBox ID="TextBox1" runat="server"
   onkeydown = "return (event.keyCode!=13);" >
</asp:TextBox>
mggSoft
  • 992
  • 2
  • 20
  • 35
1
<body onkeydown = "return (event.keyCode!=13)">
amyw
  • 11
  • 1
1

This is default behavior of a web application, you could take one of the tutorials that you found and hide that behavior, by simply abandoning the submit.

Mitchel Sellers
  • 62,228
  • 14
  • 110
  • 173