9

Have a real head-scratcher here: I have a page with several controls on it, and a ASP:Button or two. When I'm in a control, and hit the Enter key, it acts like I clicked the submit button, which I don't actually want to do at this point in time.

The button is not in a ASP:Panel with a DefaultButton set, it has no OnClientClick attribute, I can see no JavaScript that watches for the Enter key... Why is it firing?

If I create a standard .ASPX page and put a text box and a button on it, run it and hit the enter key, the button is ignored, e.g. the click event doesn't fire. This is the behaviour I expect, but am not getting.

In short, what else could cause a button to think it's the default button for the page?

Thank you,

Mike K.


Ok, here's my test page markup:

<p><asp:Label ID="uxNameL" runat="server" AssociatedControlID="uxName">Name</asp:Label><asp:TextBox ID="uxName" runat="server"></asp:TextBox></p>
<p><asp:Button id="uxSubmit" runat="server" Text="Submit" onclick="uxSubmit_Click" /></p>
<asp:Literal ID="uxOut" runat="server" EnableViewState="false"></asp:Literal>

Here's what happens in code:

    protected void uxSubmit_Click(object sender, EventArgs e)
    {
        uxOut.Text = uxName.Text;
    }

When I run the page, and enter some text in the textbox and press Enter - ha, would you look at that: In IE 8, the button event is ignored, e.g. the page just reloads, but in FF, it submits the form...

Hmmmmm.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Mike Kingscott
  • 477
  • 1
  • 5
  • 19

6 Answers6

19

Usually

<asp:Button ID="button" UseSubmitBehavior="false"/> 

works. But you may have to disable backspace in your application which takes you back 1 page in your recent history.

Nisha
  • 1,379
  • 16
  • 28
  • 1
    I think is also important to note that if you use `UseSubmitBehavior="false"`, it will not only stop it from responding to the enter button, but it will stop the button click event from being raised in the code behind when you click the button, which may not be an intended consequence. – Hawkeye Jul 26 '17 at 16:08
9

I believe (and stand to be corrected), but if you have a form, with an input button of type submit, then when you hit enter, it'll fire that button click.

Paddy
  • 33,309
  • 15
  • 79
  • 114
4

If you don't want the enter key to trigger, maybe you can add this js code to your page:

if (window.event.keyCode == 13)
{
    event.returnValue=false;
    event.cancel = true;
}

You need to call this function with the OnKeyDown handler on the <body> tag of your page.

Chris
  • 124
  • 3
3

This is the way HTML works, if <FORM> contains a <INPUT TYPE='Submit'>, hitting enter key will submit the form. A solution: How to prevent ENTER keypress to submit a web form? or here: http://www.cs.tut.fi/~jkorpela/forms/enter.html

Community
  • 1
  • 1
naivists
  • 32,681
  • 5
  • 61
  • 85
1

I have actually tried to play with it in the Firebug now and it seems that this is a standard browser behavior, i.e. it has nothing to do with javascript or ASP. If you have submit button and a textbox then it is simply submitted on enter. Change the type of the input to button to avoid it.

Tomas Vana
  • 18,317
  • 9
  • 53
  • 64
0

) HTML code:

<form method="post" action="add_data.php" id="master_form">
<input type='text' name='user-mail' />
<input type="button" value="Submit"  onclick="submitform();" />
</form>

javascript code:

function submitform()
{
document.getElementById('master_form').submit();
}