Tuesday, December 18, 2012

Prevent a postback from triggering on a button control when the Enter key is pressed

If you would like to prevent a postback from occurring on an ASP button control whenever the user presses the "Enter" key, one possible option for overriding this functionality is to add "UseSubmitButton = false" as an attribute/property of the appropriate button controls as follows:

<asp:Button id="btnSubmit" runat="server" UseSubmitBehavior="false" ... />

or

Button btnSubmit = new Button();
btnSubmit.UseSubmitBehavior = false;

In case you're curious, the reason we were asked to do this was due to a problem that our users were encountering with a postback being triggered on the "wrong" command button in our gridview control whenever a user would hit the "Enter" key.  Essentially, our gridview's rows contained several command button controls that, when clicked, would perform a variety of activities on the given row depending on which button was clicked.  Naturally, the first button on the selected row, the button with the focus, would be triggered whenever the user hit the "Enter" key; therefore, we all came to conclusion that the best route was to simply disable the "Enter" key postback functionality across all of the button controls contained in the gridview.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.