views:

370

answers:

5

I've got this weird problem, and I'm not sure whether ASP.NET or IE 7 is to blame...

The idea is this: When I have just one textbox and one (submit) button on my form, then pressing ENTER while inside the textbox will just POST the textbox'es value. The button does not get submitted and does not trigger the Click even server-side.

When I have two textboxes, then hitting ENTER in any of them will also send the button and trigger the Click event.

WTF?


Added: Seems to be an IE-related issue. Here's a simple example to demonstrate. Pay attention to the address bar and hit ENTER when in the first textbox, and then when in some of the last two.

<html>
    <body>
        <form method="GET" action="" style="display: block">
            <input type="text" id="ctl1" name="ctl1" value="">
            <input type="submit" id="ctl2" name="ctl2" value="Klik">
        </form>
        <form method="GET" action="" style="display: block">
            <input type="text" id="ctl1" name="ctl1" value="">
            <input type="text" id="ctl3" name="ctl3" value="">
            <input type="submit" id="ctl2" name="ctl2" value="Klik">
        </form>
    </body>
</html>
A: 

If you're using ASP.NET then you should only have a single form. You should also have the runat="server" attribute on any controls you want to access server-side (including the form iteself).

OK, no ASP.NET. It's not posting anything because you've got GET as the method in your form??

SillyMonkey
Scrap the ASP.NET part. This is IE-related. The example has two forms for simplicity.
Vilx-
This is an example!!!!! A distilled version of the real-life code that merely shows the issue in question! It's not even near the real-life code, so don't make any assumptions about that!And yes, the GET method is so that it's easy to observe what is submitted (just look at the address bar). The real life application has POST there.
Vilx-
A: 

The default behavior for a html form is to submit the form if any control has focus. What you need to do is trap for the ENTER key and then handle form submissions how you want to.

I have done something like this before (there are many sources for this script):

<script language="javascript"> 
function returnkey(evt)  {
    var evt = (evt) ? evt : ((event) ? event : null);
    var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
    if ((evt.keyCode == 13) && (node.type!="textarea"))  {return false;} }

document.onkeypress = returnkey; 
</script>
Joel Schlundt
A: 

If you are using asp.net, one option I've used to correct this problem is to set the button to runat="server", wrap your controls in a panel, and set the panel's defaultbutton property to the ID of the button.

Jon
If he's repeating IDs as in the example code, wouldn't that be a problem with your suggestion?
Adriano Varoli Piazza
That was a mistake in the example code. I've fixed that already.
Vilx-
+1  A: 

this thread has more solutions:

http://stackoverflow.com/questions/270494/enter-button-does-not-submit-form-ie-only-asp-net

John
+2  A: 

If I were you, I would write the form so that you don't need to worry about the click handler on the button (after all, it wasn't clicked), and also ignore the button's value when the form is submitted (never understood why buttons have values that are submitted).

As a comment to your question points out, the ENTER button's behaviour is not well standardized and you can get different behaviour in different browsers, so even if you think your page works properly it might not for all your users. Also, there are other ways forms can be submitted:

  • Enter key
  • Submit button
  • Javascript form.submit()
  • <input type="image">

For robustness, I'd use the form's onsubmit event handler to do anything that needs to happen when the form is submitted, and only use the onclick handlers to do things that need to happen when a specific button is clicked (most will just submit the form). As for the button value, I'd use an <input type="hidden"> to store whatever hidden value I want for the multiple button scenario, and not bother with the button's value.

However, if those options are not available to you, you can add a hidden input field:

<input type="text" id="h1" name="h1" value="ignore" style="display: none">

This seemed to solve the problem for me on IE8 with your sample code.

Mr. Shiny and New