We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

ASP.NET default button property does not work in FireFox

josephdaviskcrm
on
Medium Priority
1,406 Views
Last Modified: 2012-05-06
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.

Comment
Watch Question

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.

Author

Commented:
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.

Author

Commented:
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?
if you just want to stop the enter key from sumbitting buttons you can put this at the top of your page.

<script language="javascript" type="text/javascript">
function noEnter(e) {
          var evt = e ? e : window.event;
          if (evt.keyCode == 13) {
             return false;
          }
       }

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
</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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.