Data Browser - Viewing Site  Sector 23 Code Bank Logged in as:  Guest  


Prevent Enter Key within Specific Control from Submitting Form with DefaultButton
If you want the enter key to not submit a .NET form, you might override onKeyPress and have it return false if the keycode is 13.

But that does not work if the form has a button registered as a DefaultButton.

You could remove the default button, but that is not optimal if there are some controls on the form you are overriding and others where you want the defaultButton behavior.

So, to override enter for a ~specific~ control while still allowing a DefaultButton for other controls, handle the onKeyDown method instead.

Then in your handler, if you detect keyCode 13, set the following:
event.cancel = true;
event.returnValue = false;
return false;

event.cancel = false is the key thing here! If you don't have a DefaultButton, returning false would be sufficient to prevent a postback. If you have a DefaultButton, you need to set event.cancel = true.

Note: to detect keyCode 13 (enter) you can use:
(e.keyCode ? e.keyCode : e.which ? e.which : e.charCode) == 13

Created By: amos 5/29/2015 12:23:07 PM
Updated: 5/29/2015 12:24:28 PM