Re: Stop Enter Key from Clicking Button

  • Thread starter Steven Cheng[MSFT]
  • Start date
S

Steven Cheng[MSFT]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,900
Latest member
Nell636132

Latest Threads

Top