Wednesday, May 14, 2008

Controlling Which Button Fires When the User Hits Enter in ASP.NET Web Apps

If you need to control which button fires when a user hits Enter on the keyboard in an ASP.NET web application, there are two simple ways to do this.

On an ASPX page with no MasterPage and only one Button control to worry about, set the DefaultButton property of the form tag to the ID of the Button. Nice and simple.

In my specific situation, I had a MasterPage and 3 different buttons, 1 for quick search at the top, 1 for a short signup form, and 1 for a login form if the user had already signed up. Depending on which group of fields had focus, I wanted the Enter key to fire the right Button event. The solution? Wrap each in a Panel control and the set the DefaultButton property on each Panel. Again, nice and simple.

More from Bean Software where I originally saw this hint.

Here's the MSDN documentation for it as well.

No comments: