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.

{ 3 comments… read them below or add one }

1 John September 8, 2008 at 8:04 am

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

2 Jef Claes September 12, 2008 at 1:40 am

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

3 Scott May 17, 2009 at 12:59 pm

Big help, thanks

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post:

Next post: