Kent J. Chen's WebLog

...information technology, internet, and random thoughts

Trap Enter key stroke in a ASP.Net based form

Sometimes you only want the event is launched by pressing the button rather than the keystrokes, but how? I found this code from Client-side script integration in ASP.net.

  <script language="javascript">
  <!--
   var blnReturn = true;
   function trapReturn(e)
   {
    var iKeyCode = 0;
    if (window.event) iKeyCode = window.event.keyCode;
    else if (e) iKeyCode = e.which;
    blnReturn = (iKeyCode != 13);
   }
  // -->
  </script>
and add the two event handler attributes to the form tag on the web page:
  <form id="default" runat="server" onkeypress="trapReturn(event);" onsubmit="return blnReturn;">

However, it seems that it traps the mouse event as well.  It had no response while I hit the button by click the mouse left button.  So I added the third event handler attribute to the form tag, onmousedown="blnReturn=true;", which works great.

Print | posted on Sunday, June 26, 2005 1:31 AM |

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 1 and 1 and type the answer here:

Other Links

Follow me @twitter

My Recent Posts