tags:

views:

1539

answers:

3

I have a textbox whose input is being handled by jQuery.

$('input.Search').bind("keyup", updateSearchTextbox);

When I press Enter in the textbox, I get a postback, which messes everything up. How can I trap that Enter and ignore it?

(Just to preempt one possible suggestion: The textbox has to be an <asp:textbox ... /> - I can't replace it with an <input ... />.)

+9  A: 

Your browser is automatically submitting the form when you press enter. To cancel this, add return false to your updateSearchTextBox function.

if that doesn't work, try this:

<script language="JavaScript">

function disableEnterKey(e)
{
     var key;     
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox     

     return (key != 13);
}

</script>

And in your codebehind:

 textbox.Attributes.Add("OnKeyPress","return disableEnterKey(event)");
FlySwat
inline script events are naff....avoid them. I know asp.net likes them but.....grrrr the bain of my life
redsquare
+1  A: 

Ben Nadel has a great blog post on how to do this with JQuery: http://www.bennadel.com/blog/1364-Ask-Ben-Optimizing-Form-Inputs-For-Numeric-Keypad-Usage.htm

Anne Porosoff
A: 

Thanks to all who responded. Here's my new updateSearchTextbox function, which works perfectly:

updateSearchTextbox = function(e) {
    /// <summary>
    /// Handles keyup event on search textbox
    /// </summary>

    if (e.which == "13") {
        // update now
        updateConcessions();
        // Don't post back
        return (false);
    } else {
        delayedUpdateConcessions();
    }

}
Herb Caudill