Link Button on the page and set it as default button, work fine in IE but not in Mozila

后端 未结 5 1782
梦如初夏
梦如初夏 2020-12-04 02:46

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 reso

相关标签:
5条回答
  • 2020-12-04 03:03

    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.

    0 讨论(0)
  • 2020-12-04 03:13

    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.

    0 讨论(0)
  • 2020-12-04 03:14

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

    0 讨论(0)
  • 2020-12-04 03:18

    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 讨论(0)
  • 2020-12-04 03:27

    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.

    0 讨论(0)
提交回复
热议问题