Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I cancel postback when user presses return in a textbox?

Tags:

jquery

asp.net

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 ... />.)

like image 401
Herb Caudill Avatar asked Oct 16 '08 21:10

Herb Caudill


1 Answers

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)");
like image 85
FlySwat Avatar answered Sep 29 '22 19:09

FlySwat