Home | Software | WebLog | Contact | Wish List

Joel Thoms' WebLog

Here you will find random thoughts, opinions and rants from Joel Thoms. Feel free to ignore it all...
Fri, 20 Oct 2006

Simple default button
Setting a global default button for the page is very simple, all you have to do is add this code to your page... (You can get this working in ASP.net 1.x by changing 'ClientScript' to 'Page')

ClientScript.RegisterHiddenField("__EVENTTARGET", myButton.ClientID);

Though, this method will not work if you want multiple default buttons, as the single button event will always be fired.  Another caveat of this method is your JavaScript code (onclick, validators, etc) will not be fired and the form will post back to the server.

Multiple default buttons
default-bottons.gif It's not too much more difficult to implement multiple default buttons (see figure on the left) using client-side JavaScript.  I accomplish this by capturing the carriage return keypress and simulating a button click. Here's the code...

textBox.Attributes.Add("onKeyPress", "if((event.keyCode||event.which)==13){document.getElementById('" + button.ClientID + "').click();return false;}");

I've tested this code in both IE and Firefox and it seems to behave ok. I don't really care about the other browsers, but if you do, then you'll have to test.

You might also want to read my previous blog on Firefox's auto-complete JavaScript bug.

Pages: << back<< back

WebLogs powered by Joel.Net.WebLogs