Welcome to AspAdvice Sign in | Join | Help

How to prevent default button from firing

In my previous Tips and Tricks blog entry I talked about a workaround for ASP.NET not submitting when the Enter key is pressed on the keyboard. Now in this blog entry lets talk about how to prevent the button from submitting the form when you press ENTER key on the keyboard. Its very simple.. we need to capture the keycode from the onkeypress event of the form and return false

This is the code to capture the ENTER keycode...

<script language="javascript">
        function test()
        {
          if (window.event.keyCode == 13)
          {
           window.event.cancelBubble = true;
           window.event.returnValue = false; 
          }
        }
  </script>

This is the code to for the form wide onkeypress event.

<form id="Form1" onkeypress="test();" method="post" runat="server">

You could customize this.. If you want to prevent the button to submit only for a particular textbox, you could just capture the onkeypress event for that textbox and but not the whole form

Published Sunday, June 26, 2005 4:47 PM by kreddi
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: How to prevent default button from firing

Was really helpful. Was trying to negate the button click event and this came in very handy.


thanks

Tuesday, October 18, 2005 5:41 AM by Hari

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below