最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - Avoid Beep sound when enter keypress in a textbox - Stack Overflow

programmeradmin3浏览0评论

When you create an aspx page as this one:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ".dtd">

<html xmlns="">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

    </div>
    </form>
</body>
</html>

how can you avoid the beep sound that happens when you are in a textbox and hit enter.

On the other hand I would like to handle the enter onkeypress event.

Tx!

When you create an aspx page as this one:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

    </div>
    </form>
</body>
</html>

how can you avoid the beep sound that happens when you are in a textbox and hit enter.

On the other hand I would like to handle the enter onkeypress event.

Tx!

Share Improve this question asked Jan 13, 2009 at 22:51 user54517user54517 731 silver badge3 bronze badges 3
  • What browser, OS are you using? – Chuck Conway Commented Jan 13, 2009 at 23:30
  • Internet Explorer 7.0/8.0Beta and Windows Vista SP1 But it is more related to the browser. – user54517 Commented Jan 13, 2009 at 23:47
  • This would be a web solution, so I won't be able to disable it for every user. Is there any way to avoid it? – user54517 Commented Jan 13, 2009 at 23:55
Add a comment  | 

8 Answers 8

Reset to default 10

First of all, it is NOT standard behavior to get a beep when pressing enter in a textbox on a webpage. Try Google's search page, or for that matter try the Name, Email, and Home Page fields at the bottom of this page. None of them beep when pressing enter - in any browser.

To prevent the beep, handle the onKeyDown event on your <INPUT> tag and return false if the enter key is pressed:

<input type="text" name="TextBox1" value="" onKeyDown="return StopBeepOnEnter(event)" />

<script type="text/javascript">
    function StopBeepOnEnter(event)
    {
        if (event.keyCode == 13)
        {
            // Do Whatever...

            return false; // This stops the beep
        } 
    }
</script>

And here is the same thing using jQuery (I've actually tested this and it solved the problem for me):

<script type="text/javascript">
    $("#<%=TextBox1.ClientID %>").keydown(function(event)
    {
        if (event.keyCode == 13)
        {
            // Do Whatever...

            return false; // This stops the beep
        }
    });
</script>

I found the solution on this thread - credit to Tharn Jaggar:

http://codingforums.com/showthread.php?t=36491

A couple of things to note here...

  1. The alert will occur if the submit button is hidden or if there is no submit button at all. This is often the case if you hand-roll your own <a> buttons like I usually do (because default OS buttons are ugly). One thing you may want to look into is using <button type="submit"></button> because you can style them a lot more than standard input buttons and they will handle the key events organically.

  2. The alert will still occur if you intercept keyCode 13 on keyup or keydown. I don't know what it is about these events, but you have to use keypress. This is what I usually do with jQuery on a global level...

    $("input[type=text],input[type=password]").keypress(function(e) {    
      if(e.keyCode == 13) {
        $(this).closest("form").trigger("submit");
        e.preventDefault();
      }
    });
    

    And then on the page level I trap the submit event and do my validation routines...

    $("form").submit(function(e) {
      var txt = $("#txtUser");
      if(txt.val().length == 0) {
        alert("Who are you?");
        txt.focus();
        e.preventDefault();
      }
    });
    

Put this in your form tag onkeypress="if(event.keyCode==13)return false;"

If it's related to the browser that your audience is using, then they're used to it happening, because it happens on every other site besides yours. I'd say it's not worth worrying about - kind of like people with JavaScript turned off. They know they've got it turned off, and they're accustomed to sites lacking certain functionality as a result. You can't force them to turn it on, and you can't force them to turn the sound thing off. It's one of those experience comprises you've got to accept in web apps.

You have to turn it off in your sound preferences on your operating system. It's an Internet Explorer thing.

I haven't done much of anything in asp, but my first thought is that it would be similar to a Winforms application. My first step in solving it would be to set the key (code, keycode, whatever it's named in the event) var to #0 or null, as well as any vars such as e.handled in the event call prior to returning.

Finally I am able to disable the beep sound on all the components of the page while pressing Enter key in textfield or combo or any component, which was required. What I did is I added [onkeypress="if(event.keyCode==13)return false;"] in my body tag like given below:

   <body onkeypress="if(event.keyCode==13)return false;">

I used above code in my GWT GXT Application.

Krishan Babbar

KeyPressEvent.preventDefault() seems to do the trick in GWT

发布评论

评论列表(0)

  1. 暂无评论