This is one of the major UI problems which is essentially caused by ASP.NET's 'one form' rule, as Darrell points out there's a number of solutions out there but none of them really solve the whole problem (i.e., for all browsers), well now there's a great solution - from Darrell Norton's Blog (link): (reproduced here because I'll never find it again otherwise...)
Submitting default buttons when the user presses the Enter key - finally!
One of the hardest things in web development is getting a certain button to submit if you have more than one button on the page. Andy Smith, of Metabuilders fame, has one that works for late-model browsers.
Due to rather strict client requirements for a public e-commerce site, I needed something that went further back in browser history. I found a code sample from Janus Kamp Hansen that only worked for IE.
I extended it to work with Mozilla and Netscape 6+, then added some more code to get it to work with most Netscape 4+ browsers. Then I tweaked the performance a bit, and that’s it! A fast, easy-to-use code snippet to force specific submit buttons to fire from developer-determined textboxes.
First, copy this method into an easily accessible place:
public void SetDefaultButton(Page page, TextBox textControl, Button defaultButton)
{
*// Sets default buttons.* *// Originally created by Janus Kamp Hansen - http://www.kamp-hansen.dk* *// Extended by Darrell Norton - http://dotnetjunkies.com/weblog/darrell.norton/* *// -- added Mozilla support, fixed a few issues, improved performance* *string theScript = @"*<SCRIPT language=javascript>
function fnTrapKD(btn, event){
if (document.all){
if (event.keyCode == 13){
event.returnValue=false;
event.cancel = true;
btn.click();
}
}
else if (document.getElementById){
if (event.which == 13){
event.returnValue=false;
event.cancel = true;
btn.click();
}
}
else if(document.layers){
if(event.which == 13){
event.returnValue=false;
event.cancel = true;
btn.click();
}
}
}
</SCRIPT>";
*Page.RegisterStartupScript("ForceDefaultToScript", theScript);* *textControl.Attributes.Add("onkeydown", "fnTrapKD(" + defaultButton.ClientID + ",event)");*}
This code registers the given script with the Page. Then it adds an attribute to the textbox, which in this case is an onKeyDown event that calls the fnTrapKD function with the button’s clientID (it’s HTML ID in the rendered HTML) and the event. I had to pass the event in because Netscape/Mozilla browsers can’t catch this kind of event unless you pass it in to the function. IE can access the event from the document object, but we want something nice and cross-browser compatible.
The document.all if statement covers IE. The document.getElementById covers Netscape 6+ and Mozilla browsers. The document.layers if statement works with Netscape 4+.
Now add a line of code linking each textbox that you want with a certain submit button. For example:
*SetDefaultButton(this, TextBox1, Button1);* *SetDefaultButton(this, TextBox2, Button2);* *SetDefaultButton(this, TextBox3, Button3);*I usually put that in the Page_Load, since the Page.RegisterStartupScript method will ignore duplicate scripts. Now it is simply a matter of making a call to SetDefaultButton for each textbox-submit button association.
I haven’t tested this extensively yet, and some of the JavaScript may be unnecessary. And considering the number of Netscape 4 versions (and their bugginess), it probably doesn’t work on all of them. But it works (for now). And at least making an attempt at Netscape 4+ and IE 5+ will cover 99% of my site’s web browsers.
© 2025 Scott Galloway — Unlicense — All content and source code on this site is free to use, copy, modify, and sell.