Richard Szalay

Thursday, July 13, 2006

Workaround: Hitting enter to submit a form in Internet Explorer does not fire an ASP.NET Button Click / Command event

There's a funny bug in Internet Explorer that causes it to not send FirstButtonName=FirstButtonValue as a form value when there is only 1 input type='text' on the page. This results in the ASP.NET Button controls not firing their Click or Command events.

Now I've spotted a few workarounds to this problem before, but they all involve javascript. Not very elegant, and doesn't help those with no script.

The work around I have come up with is to add a hidden text field just after the <form> tag. If there are no text fields on the form, then the user cannot hit enter to submit the form and if there is, then there is technically now 2 - meaning the bug won't occur:

<form runat="server">
<!--[if IE]><input type="text" style="display: none;" disabled="disabled" size="1" /><!-->

A few notes:

  • The 'if IE' ensures it only gets picked up by Internet Explorer to begin with
  • The disabled/size attributes are for browsers with no CSS so that at least its as small as possible and the user cannot focus the cursor into it.