Friday 3 August 2007

Making asp:Panel DefaultButton work for a LinkButton in FireFox

The DefaultButton property of an asp:Panel is great, until you want to use a LinkButton or ImageButton. It just doesn't work in FireFox. This is because FireFox does not have a click method on it's HTML anchors. Fortunately a solution is at hand. Using this JavaScript you can add a click method to anchors that are missing one i.e. those in FireFox.
function AddClickMethod (clientId)
{   
      var anchor = document.getElementById (clientId);
      // define a click function for firefox
      if (typeof(anchor.click) == "undefined")
      {
           anchor.click = function ()
           {
               eval(this.href.replace("javascript:", ""));
           }
      }
}
You can then link the function to your default link button using:
AddClickMethod ("<%= myLinkButton.ClientID %>");
... and presto, a working asp:Panel default button!

Notes

Caveat: This does not work if you have validation controls with client script enabled. You can disable client script on a control with enableClientScript=false. I haven't tried it but I imagine it works with asp:ImageButton too.

No comments :