Exploring the depths and potentials of ASP.NET RSS 2.0 or Subscribe to .BenRush by Email
 Monday, July 16, 2007

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=undefined

This 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.



kick it on DotNetKicks.com
Monday, July 16, 2007 2:49:54 PM (Central Standard Time, UTC-06:00)  #    Comments [0] - Trackback
.Net Runtime | AJAX | ASP.Net | JavaScript
Tracked by:
"http://beuchelt.blogdns.net:8080/BlogEngineUpgrade.aspx" (http://beuchelt.blogd... [Pingback]
"Blog Engine Upgrade" (Web Services Contraptions) [Trackback]

Computers Blogs - Blog Top Sites

Archive
<January 2009>
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567
Blogroll
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2009
Benjamin Rush
Sign In
Statistics
Total Posts: 444
This Year: 0
This Month: 0
This Week: 0
Comments: 128
Themes
Pick a theme:
All Content © 2009, Benjamin Rush
DasBlog theme 'Business' created by Christoph De Baene (delarou)