3

I have a link button on the page and set it as default button, It works fine in IE but not working in Mozila Firefox. Does anybody have any clue how to resolve this issue?

animuson
  • 53,861
  • 28
  • 137
  • 147
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
  • What I have check it, Link Button could not be set as default in Mozila – Muhammad Akhtar Jun 02 '09 at 11:17
  • No my friend; you have to set the DefaultButton property of the HtmlForm (asp:Form) on the page. Set it to the id of the LinkButton and it will work automatically. – Kirtan Jun 02 '09 at 13:02
  • And if you want multiple default buttons on a single form, use an asp:Panel control; group your controls accordingly and set the DefaultButton property of the panel. Just be sure to put the button in that respective panels. – Kirtan Jun 02 '09 at 13:04
  • Thanks, that's right, now event is fired and working but now get another problem doing so, I am calling an encryption method on OnClientClick, that's is called when I click on the link button, but not not called when I hit enter button – Muhammad Akhtar Jun 02 '09 at 13:23
  • I think what happen here is, when hitting enter button, javascript WebForm_FireDefaultButton method called and click event is fired and it ignores others event like onclientClick. – Muhammad Akhtar Jun 02 '09 at 13:41

5 Answers5

14

The DefaultButton property is not supported for use with a LinkButton. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.panel.defaultbutton.aspx

The easiest solution is to include a dummy Button on your page with style="display:none", set that as the defaultbutton and give it the same event handlers as your LinkButton.

Protector one
  • 6,926
  • 5
  • 62
  • 86
  • 2
    It might have to do with the way it's implemented… I noticed the Javascript behind the functionality uses the `click` property of the button. However, whilst IE implements a click property for links, other browsers do not. This is why the current implementation of the DefaultButton functionality only works for LinkButtons in IE. – Protector one May 12 '11 at 17:08
  • 3
    Wow... this is by FAR the simplest, easiest-to-implement workaround. Thanks! – David Oct 05 '11 at 17:41
2

I had this kind of issue with FF3 and ASP.NET linkbuttons. This seems to be a bug with FF3 (not sure), but the script that fixed is given below:

var __defaultFired = false;

function WebForm_FireDefaultButton(event, target) {
    var element = event.target || event.srcElement;

    if (!__defaultFired && event.keyCode == 13 && !(element && (element.tagName.toLowerCase() == "textarea"))) {
        var defaultButton;

        if (__nonMSDOMBrowser)
            defaultButton = document.getElementById(target);
        else
            defaultButton = document.all[target];

        if (defaultButton) {
            if(typeof(defaultButton.click) != "undefined")
                defaultButton.click();
            else
                eval(unescape(defaultButton.href.replace("javascript:", "")));

            event.cancelBubble = true;

            if (event.stopPropagation) event.stopPropagation();
            return false;
        }
    }
    return true;
}

Keep it at the end of the page so that it overrides the WebForm_FireDefaultButton method rendered by ASP.NET.

Kirtan
  • 21,295
  • 6
  • 46
  • 61
1

My first Stack Overflow write, wow :-)

asp.net:

<asp:Panel runat="server" DefaultButton="lbHello">
    First name: <asp:TextBox runat="server" ID="txtFirstName" />
    <asp:LinkButton ID="lbHello" Cssclass="button" runat="server" Text="Click me" OnClick="lbHello_Click" />
</asp:Panel>

js:

$(document).ready(function () { $('.button').eventClick(); });

$.fn.eventClick = function() {
    function eventClick(a) { 
            if (a && typeof (a.click) == 'undefined') {
                a.click = function () {
                    var result = true;
                    if (a.onclick) result = a.onclick();
                    if (typeof (result) == 'undefined' || result) {
                        eval(a.getAttribute('href'));
                    }
                }
            }
        }
        return eventClick($(this).get(0));
}
0

work only first time we press enter in textbox. After adding some text in textbox and then pressing enter ,default button will not fire.

rakesh
  • 113
  • 3
  • 12
0

I think its very simple, just add onkeypress js event of textbox where post back is required.

txtUserName.Attributes.Add("onKeyPress", "javascript:if (event.keyCode == 13)
 __doPostBack('" + btnLogin.UniqueID + "','')");

hope this will be helpful.