views:

306

answers:

2

I have a submit button and back button in my asp.net webform. I need to use the submit button when pressing enter, but it's going to the back button instead.

Please help...

+4  A: 

Wrap your section in a Panel - then you can use the DefaultButton property to set your submit button to be the default when enter is pressed.

<asp:Panel runat="server" ID="MyPanel" DefaultButton="MySubmitButton">
    ... put your form here
    <asp:Button runat="server" ID="MyCancelButton" Text="Cancel" />
    <asp:Button runat="server" ID="MySubmitButton" Text="Submit" />
</asp:Panel>
Scott Ivey
Can i use "UseSubmitBehaviour" property of button ?i am using a view now there is no panel
No - that property just says whether to use the browsers submit behavior or use the standard asp.net postback behavior. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.usesubmitbehavior.aspx
Scott Ivey
+1  A: 

You need to set the default button for you form or panel

<form id="form1" runat="server" defaultbutton="MySubmitButton">

Or intercept the key presses in JS:

Example uses YUI to help :)

YAHOO.util.Event.onDOMReady(function() {
    YAHOO.util.Event.addListener(document, "keydown", enterKeyPress);

});

function enterKeyPress(ev) {

    ev || (ev = window.event);
    var code = (ev.keyCode ? ev.keyCode : (ev.charCode ? ev.charCode : ev.which));

    // pressed ENTER get TAB
    if (code == 13) {
        try {

          var button = document.getElementById("you button client id");
          if (button != null) button.click();

        }
        catch (Error) {
            logError(Error);
        }
        YAHOO.util.Event.preventDefault(ev);
    }

}
BigBlondeViking
+1 for the defaultbutton. I had an <asp:imageButton/> which was firing instead of the login button when the user hit Enter. Setting the default button on the form did the trick.
Randy Eppinger