There seems to be a lot of confusion out there with regards to getting the ENTER key to submit a form. I'm here to tell you that the best solution I have found (and the one I use) is to drop whatever TextBox controls I'm using and the final Submit button into a panel control, and then set the DefaultButton property of the panel to the button to be clicked when ENTER is pressed. For example:
<asp:Panel ID="Panel1" runat="server" DefaultButton="Button_Search" Height="50px"
Width="329px">When focus is in a TextBox control inside the Panel and the ENTER key is struck, Button_Search will be "clicked" or invoked by the browser to post back the form data. The result is the feel of google's search engine where you simply type in the text box and hit ENTER to start the search.
A default button can also be supplied for the Form object itself, and this is set as a property on the form object too.
One thing to note is that the default behavior when a form is submitted through an ASP.NET button control is that the __EVENTTARGET or "source" of the form post is set to nothing. For example, by clicking a submit button manually, the Form variables posted look like this:
__LASTFOCUS=&__EVENTTARGET=&__EVENTARGUMENT=However, by having the ENTER key cause the form post, the "cause" of the form post is manually set to actually be the button:
__LASTFOCUS=&__EVENTTARGET=Button_Search&__EVENTARGUMENT=undefinedThis causes ASP.NET to have a fit if you do not have the following option set on the page (which can also be set through the web.config file):
<%@ Page EnableEventValidation="false"
The error you will see will be something like this:
Invalid postback or callback argument. Event validation
is enabled using <pages enableEventValidation="true"/> in
configuration or <%@ Page EnableEventValidation="true" %> in a
page. For security purposes, this feature verifies that arguments to
postback or callback events originate from the server control that
originally rendered them. If the data is valid and expected, use the
ClientScriptManager.RegisterForEventValidation method in order to
register the postback or callback data for validation.
Care should be had when setting the EnableEventValidation to false as it is set to True for security reasons.