Link to home
Start Free TrialLog in
Avatar of josephdaviskcrm
josephdaviskcrmFlag for United States of America

asked on

ASP.NET default button property does not work in FireFox

In ASP.NET there is a property of the <asp:panel> control called default button.  If an element within that panel control has the focus at the time the enter button is clicked by the user, then the control with the id specified by the defualt button property of the control is triggered.  The ASP.NET syntax looks as follows...

<asp:Panel runat="server" ID="pnlNewContact" CssClass="addContactPanel" DefaultButton="lbAddNewContact">
...
...
...
</asp:Panel>

This seems to work fine in IE, but it is not working in FireFox.  This is something very damaging to the page I'm working on as the alternate defualt button that is selected by no doing of my own is a delete button.

Is there any way to make this work in FireFox?  Or some other way to control the event that is fired when the enter button is clicked.

Avatar of copyPasteGhost
copyPasteGhost
Flag of Canada image

javascript:

function clickButton(e, buttonid) {
            var evt = e ? e : window.event;
            var bt = document.getElementById(buttonid);
            if (bt) {
                if (evt.keyCode == 13) {
                    bt.click();
                    return false;
                }
            }
        }


C#

txtTextBox.Attributes.Add("onkeypress", "return clickButton(event,'" + btnToSubmitPage.ClientID + "')");

That should work.
Avatar of josephdaviskcrm

ASKER

I'm not sure I'm totally following you.  If this function were to be added to the keypress event in the aspx file, how would it look?

Also, I'm wondering if this can be added to buttons instead of to a text box so that I can program into the button to test and see if it was triggered by an enter click and if so to do nothing.
<script language="javascript" type="text/javascript">
function clickButton(e, buttonid) {
            var evt = e ? e : window.event;
            var bt = document.getElementById(buttonid);
            if (bt) {
                if (evt.keyCode == 13) {
                    bt.click();
                    return false;
                }
            }
        }
</script>
yes it can give it a shot. let me know if there are any problems.
So does the return false cancel out the submit functionality?  Or what exactly needs to happen here?  I have multiple buttons on my page that act as instant delete buttons for the records they correspond to.  I don't want them ever to be activated by the enter key press event.  Here is the function that is called when any of these is clicked...

function DeleteContact(Email) {
        var xmlHttpObj = CreateXmlHttpRequestObject();
        xmlHttpObj.onreadystatechange = function() {
            if (xmlHttpObj.readyState == 4) {
                UpdateAddressBook()
            }
        }
        xmlHttpObj.open("GET", "CustomAjaxResponse.aspx?AR=DeleteContact&contact=" + Email + "&r=" + (Math.random() * 9999999), true);
        xmlHttpObj.send(null);
        return false;
    }

So how would I encorporate your function into this one?
ASKER CERTIFIED SOLUTION
Avatar of copyPasteGhost
copyPasteGhost
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
</script>

ANd then in your code behind...

btn1.Attributes.Add("onkeypress", "return noEnter()");
btn2.Attributes.Add("onkeypress", "return noEnter()");
btn3.Attributes.Add("onkeypress", "return noEnter()");
btn4.Attributes.Add("onkeypress", "return noEnter()");

Ghost