Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enter key triggering button click - but I don't want it to

Tags:

asp.net

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.

like image 270
Mike Kingscott Avatar asked Feb 02 '10 15:02

Mike Kingscott


People also ask

How do you trigger button click on enter?

To trigger a click button on ENTER key, We can use any of the keyup(), keydown() and keypress() events of jQuery. keyup(): This event occurs when a keyboard key is released. The method either triggers the keyup event, or to run a function when a keyup event occurs.

How do you trigger a button click on Enter in react?

Using the onKeypress event The onKeyPress event is fired when a user presses the key on a keyboard, so that by using this we can trigger the button click by pressing a Enter key. The keyCode for the Enter key is 13.

How do you trigger HTML button after hitting Enter button in textbox using JavaScript?

How to trigger HTML button after hitting enter button in textbox using JavaScript ? Given an HTML document containing text area and the task is to trigger the button when the user hit Enter button. We can do it by using “keyup”, “keydown”, or “keypress” event listener on textbox depending on the need.


1 Answers

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.

like image 130
Paddy Avatar answered Oct 13 '22 14:10

Paddy