How to submit form pressing enter key in ASP.NET form

davearia

Centurion
Joined
Jan 4, 2005
Messages
184
Hi All,

I am trying to get an ASP.NET form to submit data on the form by pressing the enter key as opposed to the mouse being used to the press the button on the screen.

From what I have found on this subject it seems as though I will have to write some javascript that will provide client script code to monitor key strokes.

But I am struggling in dealing with this. Can somebody please give me some help here?

Thanks, Dave. :D
 
Here is how I managed it, this goes right at the start of your <head> tag:
Code:
<SCRIPT LANGUAGE="javascript">
if (document.all)
{ 
    document.onkeydown = function ()
    {			
        var key_enter= 13; 
        if (key_enter==event.keyCode)
        {
            document.getElementById('button1').click()
        }
    }
}
</SCRIPT>
 
Code:
<script language="javascript">
   function enter_key() {
      if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13))
      {document.Form1.btnSubmit.focus();} 
   }
</script>

txtTextbox.Attributes.Add("onkeydown", "enter_key();")
 
Back
Top