mostlylucid

scott galloway's personal blog...
posts - 911, comments - 723, trackbacks - 11

My Links

News

Archives

Post Categories

Misc. Coding

Really nice cross-browser method of doing the 'DefaultButtons' thing - so that when you hit enter, the correct button 'clicks'

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.

Print | posted on Thursday, March 04, 2004 11:37 AM | Filed Under [ ASP.NET ]

Feedback

Gravatar

# re: Really nice cross-browser method of doing the 'DefaultButtons' thing - so that when you hit enter, the correct button 'clicks'

Glad you like it. Hopefully I will be able to improve it as error reports come in. :)
3/4/2004 3:53 PM | Darrell
Gravatar

# re: Really nice cross-browser method of doing the 'DefaultButtons' thing - so that when you hit enter, the correct button 'clicks'

This wont help if client-side script is disabled?!
3/5/2004 3:36 PM | Ahmed
Gravatar

# re: Really nice cross-browser method of doing the 'DefaultButtons' thing - so that when you hit enter, the correct button 'clicks'

No it won't...what might though is something like Paul Wilson's (http://www.wilsondotnet.com/Controls/) web form control, that lets you use multiple forms
3/5/2004 3:47 PM | Scott Galloway
Gravatar

# re: Really nice cross-browser method of doing the 'DefaultButtons' thing - so that when you hit enter, the correct button 'clicks'

Don't use the RegisterStartUpScript, when you can use the Page.RegisterClientScript (check exact name in vs). Though they function in a similar way, I thought ..StartUp.. was for registering script blocks to be called when the page loaded.
3/8/2004 2:33 PM | John
Comments have been closed on this topic.

Powered by: