views:

573

answers:

3

I have a simple form written in asp.net/C# and when trying to hit enter while in the form's input box doesn't submit the form for some reason. I had implemented a fix for a previous bug where pressing enter would merely refresh the page without submitting the form data but now pressing enter just does nothing, the fix is below:

<div style="display: none">
    <input type="text" name="hiddenText" />
</div>

anybody know about a fix for this or a workaround?

+2  A: 

You can specify a default button for a form, which means hitting enter on any input control will fire that button (i.e. target the submit button). I haven't heard of this not working in any specific browser. This should eliminate your need for a workaround/hack.

<form id="form1" runat="server">
    <asp:Panel ID="pnlFormContents" runat="server" DefaultButton="btnSubmit">
        <!-- add some input controls as needed -->
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"/>
    </asp:Panel>
</form>

Hope this helps...

KP
Great minds think alike, I think we submitted at the same time. +1
Kyle B.
+2  A: 

I'm assuming you have a button somewhere on your page, as well as an event handler for it.

Have you tried wrapping your form (with the button) inside a Panel control and setting the default button attribute?

i.e.

<asp:Panel id="pnlMyForm" runat="server" DefaultButton="btnMyButton">
<asp:textbox id="txtInput" runat="server" />
<asp:Button id="btnMyButton" text="Submit" runat="server" />
</asp:Panel>
Kyle B.
lol 11 seconds apart actually :) +1
KP
thanks to both kyle/kevin, worked perfectly, just had to remove my "fix" for the old bug, upvoted kevin's answer for some rep for him too
Jimmy
A: 

I don't remember the specifics of the rules, but most browsers have the capability of submitting forms when ENTER is pressed if conditions are met. I think it had to do with whether you had 1 or more-than-one field, or whether or not there was at least one submit button (even if you hide it). I've done it in a site I recently did, but I don't have the code handy, but I can tell you it works without any special scripting. Check this posting for more details:

http://manfred.dschini.org/2007/09/20/submit-form-on-enter-key/

Trinition