Is it possible to cancel the postback when the user clicks the back button in the text box?

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

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

When I press Enter in the text box, I get a postback that will ruin everything. How can I catch this input and ignore it?

(Just to preempt one possible sentence: the text field should be <asp:textbox ... /> - I cannot replace it with <input ... /> .)

+6
jquery
source share
3 answers

Your browser will automatically submit the form when you press the enter button. To undo this, add return false to the updateSearchTextBox function.

if this does not work, try the following:

 <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 code:

  textbox.Attributes.Add("OnKeyPress","return disableEnterKey(event)"); 
+13
source share

Ben Nadel has an excellent blog post on how to do this using jQuery: http://www.bennadel.com/blog/1364-Ask-Ben-Optimizing-Form-Inputs-For-Numeric-Keypad-Usage.htm

+1
source share

Thanks to all who responded. Here is my new updateSearchTextbox function that works fine:

 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(); } } 
0
source share

All Articles