Setting the Default Button on Pressing Enter

On a page with a single text box and a button, place your cursor in the text box and press enter. The page will submit back to itself but not hit the click event behind your submit button as expected.

See here: for a full discussion of the issue.

The fix for this simple problem is to add another text box like so:

<asp:textbox runat="server" style="visibility:hidden; display:none;>

This however only fixes the page posting to itself, we now need to get the page to fire the default button’s click event.My problem is further complicated by these facts:

1. The page is using a master page with the main content area containing the text box and a footer content area containing the button.

2. The button is not an  <asp:button…  but a custom button control that uses an asp:hyperlink control.

There is a DefaultButton property that can be set on the form. The caveat being when using master pages you must use the uniqueID of the control. So in my page onload event the following line of code needs to be added.

this.Page.Form.DefaultButton = btnOK.UniqueID;

This causes the btnOk “Click” event to be fired.


1 thought on “Setting the Default Button on Pressing Enter

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.