Stop Enter Key from Clicking Button

  • Thread starter Thread starter Barry
  • Start date Start date
B

Barry

How can I stop the EnterKey from clicking the button on my form? I am using
a scanner to enter one of the textboxes and it keeps sending an Enter which
clicks the button on my form before the rest of the data has been entered.
 
This is a client side issue but you can use

if (window.event.keyCode == 13) {
//handle the enter key here
}

in the body_onkeypress event.

Jay
 
Thanks Jay. I have never written any client side code. Can you show me how
to code this set up the event (there is not body_onkeypress event in my
code)"

--
Barry Fitzgerald
Jason MacKenzie said:
This is a client side issue but you can use

if (window.event.keyCode == 13) {
//handle the enter key here
}

in the body_onkeypress event.

Jay
 
Sure. I didn't actually test this so if you have a problem, let me know and
I'll actually test the solution next time.

<body onkeypress="CaptureEnter();">

</body>

<script language=javascript>

function CaptureEnter()
{
if (window.event.keyCode == 13) {
return false
}

}

</script>

Barry said:
Thanks Jay. I have never written any client side code. Can you show me how
to code this set up the event (there is not body_onkeypress event in my
code)"
 
I'm pretty sure I did this right. I changed my <body> to look like yours and
added the <script ... code after the </body>. I understand what this is
supposed to do but it doesn't appear to do anything

If I put a breakpoint on the function statement it never gets there???
 
Hi Barry,

This problem is a known behavior of the IE, the first submit button on the
page will be fired(it is always on focus) when the user hit the enter key.
I think Jason's suggestion that use the client script to capture the
keypress event and cancel it is reasonable. And here is a test page I made
and it worked well. Please refer to it to see whether it works on your side.
=============aspx page================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>EnterKey</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<script language=javascript>
function CaptureEnter()
{
if (window.event.keyCode == 13)
{
window.event.cancelBubble = true;
window.event.returnValue = false;
}
}
</script>
</HEAD>
<body onkeypress="CaptureEnter()">
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>
<asp:TextBox id="TextBox2" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>
<asp:TextBox id="TextBox3" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>
<asp:TextBox id="TextBox4" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>
<asp:Button id="Button1" runat="server" Text="Button1"></asp:Button>
<asp:Button id="Button2" runat="server"
Text="Button2"></asp:Button></td>
</tr>
</table>
</form>
</body>
</HTML>
==========code behind page class=============
public class EnterKey : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.TextBox TextBox4;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
Response.Write("<br>Button1 is clicked at: " +
DateTime.Now.ToLongTimeString());
}

private void Button2_Click(object sender, System.EventArgs e)
{
Response.Write("<br>Button2 is clicked at: " +
DateTime.Now.ToLongTimeString());
}
}
================================

In addition, here are some former threads dicussing on the similiar
problem, you can view them via the following google weblink:

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&edition=us&threa
dm=%24EkozUP9DHA.2052%40cpmsftngxa07.phx.gbl&rnum=1&prev=/groups%3Fq%3Dpreve
nt%2Benter%2Bkey%2Bsteven%2Bcheng%26ie%3DUTF-8%26oe%3DUTF-8%26edition%3Dus%2
6hl%3Den

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&edition=us&threa
dm=Qs2RG6hDEHA.2304%40cpmsftngxa06.phx.gbl&rnum=6&prev=/groups%3Fhl%3Den%26l
r%3D%26ie%3DUTF-8%26oe%3DUTF-8%26edition%3Dus%26q%3Denter%2Bkey%2Bsteven%2Bc
heng

Hope also helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Back
Top