Home > ASP .Net > Setting the Default Button on Pressing Enter

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: http://aspnet.4guysfromrolla.com/articles/060805-1.aspx 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.


Categories: ASP .Net
  1. Jeen
    1 August, 2008 at 6:40 am

    Hi jcrawfor74,
    Useful information. Thank you very much….

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: