Tuesday, November 15, 2005

Button Defaults in HTML

I'm going to forget this, so . . .

Managing the active or default button on web page is tricky, depending on what kind of button you are using. At least in IE.

Submit buttons (input type submit) don't give you any options. When your form gets focus, the first submit button gets the equivalent of a setActive(). You can try to call setActive() on subsequent submit buttons, but it doesn't make any difference.

This is a bummer if you've got a wizard interface, with Cancel, Back and Next all in a row at the bottom of your form. Note that System.Web.UI.WebControls.Button in ASP.NET renders to one of these buttons.

However, buttons rendered with the button tag are more forgiving. They haven't got any default behavior, but at least you can roll your own by monitoring keystrokes in keypress. To get my wizard controls buttons to function the way I want, I've created a System.Web.UI.WebControls.Button work-alike control which renders to a button tag instead of an input tag.

While we're at this, note that setActive() and focus() are two distinct methods, and accomplish different ends. I want my default button to be setActive() when I'm in any of the controls in my form, but the specific control I'm on has the focus.