Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

Default Button in ASP.NET

Here's a good one. Does anyone know how to make a button on an ASP.NET page respond to the enter key? The browsers support a default button within forms but it seems that because each ASP.NET page has a form around everything the enter key simply posts the entire page back.
I noticed that gotdotnet has disabled the enter key in much of their search inputs. I hope thats not the only option....

Brendan Erwin
Friday, March 7, 2003

BTW, I want to do this without 'hacking' the html and adding my own client script.
I just went and looked closer at gotdotnet and found that I was partially wrong... They didnt disable the enter key but they DO have javascript in place that checks the last keycode on certain text fields. The code looks like it may have been generated which makes me think that there might be a tool in VS.NET.

Brendan Erwin
Friday, March 7, 2003

Check out:

Duncan Smart
Saturday, March 8, 2003


Brendan Erwin
Saturday, March 8, 2003

You always do get the whole form back in ASP.NET, because that's the way the system was designed. You use events to discover which button was clicked. (This is one the cases against client-side validation, in my mind, since the client side validation now needs to know which "part" of the form neesd to be validated).

The only way to do it is with client side code. The link provided produces quite a bit of client-side JavaScript, it you look.

Brad (
Sunday, March 9, 2003

I always put a dummy button on the form too, and give it

style="float : left;position:absolute;left:-300px;width:0px;height:0px"

Since the browsers will automatically "click" the first button on the form when you hit enter in a textbox.

The DefaultButton stuff from MetaBuilders works well though.

Richard Ponton
Thursday, March 13, 2003

if you were going to do it that way then why not just do:

Duncan Smart
Friday, March 14, 2003

because browsers are smart enough not to "click" on buttons with display : none (or stupid enough), which defeats the purpose of having the dummybutton there in the first place.

Richard Ponton
Monday, March 17, 2003

*  Recent Topics

*  Fog Creek Home