In browsing the asp.net/forums, I have noticed a recurring issue. Pressing the Enter key on a form with more than one submit button fires the wrong event. Since, in ASP.NET, you can have only one form on a page, pressing Enter to submit a form will fire the event of the first button on the page. This is not always the behavior you intend. In many cases there are many command buttons on a page. The desired effect would be to fire the appropriate command button based on the fields that are directly related to it. This may be
a re-post of this topic but below is the code to resolve this God foresaking issue.
Below is the code and an example for you to follow.
<script language="c#" runat="server">
void Page_Load (object sender, EventArgs e)
{
}
void Fire1_Command (object sender, EventArgs e)
{ Status.Text = Fire1.Text;
}
void Fire2_Command (object sender, EventArgs e)
{ Status.Text = Fire2.Text;
}
< SPAN>script>
<html>
<script language="JavaScript">
function KeyDownHandler(btn)
{ // process only the Enter key
if (event.keyCode == 13)
{ // cancel the default submit
event.returnValue = false;
event.cancel = true;
var obj = document.getElementById(btn);
obj.click();
}
}
< SPAN>script>
<body>
<form method="post" runat="server">
<asp:TextBox ID="Fire1" Runat="server"
onKeyPress="KeyDownHandler('FireButton1');" />
<asp:Button Runat="server" OnClick="Fire1_Command"
Text="Fire Button 1" ID="FireButton1" /><br>
<asp:TextBox ID="Fire2" Runat="server"
onKeyPress="KeyDownHandler('FireButton2');" />
<asp:Button Runat="server" OnClick="Fire2_Command"
Text="Fire Button 2" ID="FireButton2" /><p>
<asp:Label ID="Status" Runat="server" />
< SPAN>form>
</< SPAN>body>
< SPAN>html>
This code should get you on your way. I would love to hear back how it help you. Be sure to post!