ASP.NET Press Enter to Submit Form via TextBox

by Otto on November 20, 2007

I was working on an internal web site today and I needed to allow the user to enter some information into a asp:TextBox control and press enter to submit the form. 

It looks something like this:

DefaultButton

I’ve done this in the past and decided to do a quick Google to find out how to do it again (it would have been great if I remembered how to do it).  I figured searching Google was faster than searching my code.

What did I find?  A lot of talk about writing javascript to capture when the person pressed enter, then having javascript call your button click event, etc etc etc.  Sure, that stuff works, but it seems like you’re going through a ton of work to do something that should be extremely simple.

It is simple and here is what I consider the easiest way to click a button when a user presses enter while in a TextBox control.  Wrap the TextBox and Button (or ImageButton) in a asp:Panel and set the Panel’s DefaultButton property.  If you don’t want the ImageButton to show up, set the Height and Width to “0px”.

Leave a comment if you want sample code.

{ 10 comments }

John September 8, 2008 at 8:04 am

Thanks a lot. Sick of all these javascripts. This works perfectly.

Jef Claes September 12, 2008 at 1:40 am

Thanks. This works perfect. It’s funny how many awesome features stay hidden.

Scott May 17, 2009 at 12:59 pm

Big help, thanks

falan May 10, 2010 at 3:04 am

thanks

Yuriy August 12, 2010 at 8:09 am

Thanks a lot.

Matthias September 9, 2010 at 3:30 am

Great and simple way, thanks a lot!

ly April 15, 2012 at 6:16 pm

Thank you

jon May 29, 2012 at 5:56 am

thanks for the nice trick – works great

Anton September 23, 2012 at 1:27 pm

Thanks man! MUCH appreciated!

Muhammad September 28, 2012 at 3:28 am

Excellent…

Comments on this entry are closed.

Previous post:

Next post: