asp.netkeypressdefaultbutton

How to disable buttons get triggered with <Enter> on aspx-pages


I have a aspx-page with several textboxes and buttons. However, when the cursor is in a textbox, one of the buttons is still "in focus". So when I hit the Enter-key the button is pressed. What I want is to disable the ability to trigger buttons with the Enter-key, or at least when the cursor is in a textbox.


Solution

  • The solution for me was this:

    TextBox.Attributes.Add("onkeypress", "return event.keyCode!=13");
    

    on every textboxs and radiobuttons to prevent the enter key to submit the form.