hightow
hightow

Reputation: 779

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.

Upvotes: 2

Views: 7316

Answers (4)

Dmitry Shashurov
Dmitry Shashurov

Reputation: 1734

To stop form submission on Enter key, add next code in Page or MasterPage:

<script type="text/javascript">        
        document.onkeypress = function(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 == "text")) { return false; }
        };
</script>

Upvotes: 0

hightow
hightow

Reputation: 779

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.

Upvotes: 4

Sjoerd
Sjoerd

Reputation: 75679

You can try Button.UseSubmitBehavior, or disable the submit of the form using Javascript.

Upvotes: 2

sandyiit
sandyiit

Reputation: 1715

Set of the focus on the textbox where the curson is when the page is loaded - window.onLoad() method.

If its the only submit button in you form then it will be triggered automatically when you press enter. To disable it , you have to mark its as disabled.

Upvotes: 0

Related Questions