52198

jquery asp.net, stopping postback

Question:

I have a text box and I want the user to be able to type in the TextBox and when they hit enter, I want jQuery to make a ajax call to a web method.<br /> The problem is, when user hits the enter, the method is called but then the page refreshes due to the return. I've tried using return false but with no results.

<strong>Here is the code:</strong>

<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="X-UA-Compatible" content="IE=7" /> <script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script> <script language="javascript"> function serviceCall(getText) { $.ajax({ type: "POST", url: 'ActiveDirectoryAutoFillWebService.asmx/TestMethod', data: "{'getId':'+ getText +'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { $("#divResult").html(msg.d); }, error: function (e) { $("#divResult").html("WebSerivce unreachable " + e.name + " " + e.message); } }); // return false; This does not work } function searchKeyPress(e) { if (typeof e == 'undefined' && window.event) { e = window.event; } if (e.keyCode == 13) { serviceCall(e) // return false; This does not work } } </script> </head> <body> <form id="form1" runat="server"> <div id="divResult" style="margin-top: 20px;"></div> <asp:TextBox ID="tbSearchName" runat="server" onKeyPress="searchKeyPress(event); "></asp:TextBox> </form> </body> </html>

If anyone knows how I can accomplish this, please let me know.

Answer1:

Have a look at the following functions on the event object:

<ul><li><a href="http://api.jquery.com/event.stopImmediatePropagation/" rel="nofollow">stopImmediatePropagation</a></li> <li><a href="http://api.jquery.com/event.stopPropagation/" rel="nofollow">stopPropagation</a></li> <li><a href="http://api.jquery.com/event.preventDefault/" rel="nofollow">preventDefault</a></li> </ul>

In your case, you can use

e.preventDefault()

This will stop the default action from happening.

Recommend

  • Prevent date and time insert to the database
  • Openstack.Net SDK cannot access services
  • Javascript snippet not working (missing lib?)
  • How to use compare validator to compare the data between two dropdownlists values?
  • Overriding window.location on Webbrowser Control
  • How to use both ga.js and analytics.js?
  • How to change display text in item template based on eval in gridview?
  • Kinect API written solely in Java without native functions?
  • Jquery Google Maps V3 - Information window is always fixed to one marker
  • How to center a menu using flexbox [closed]
  • reduce/reduce conflicts using ocamlyacc
  • Can you use DataSet and DataTables in a Portable Class Library
  • Checkpointing In ALS Spark Scala
  • jinja2 template not found and internal server error
  • ASP.NET RegularExpressionValidator, validate on a non-match?
  • Heroku push rejected - Hartl's Rails 3.2 tutorial
  • How secure are apple APNS push notifications?
  • Feature detection of foreignObject in SVG
  • Why does the font in these TD elements render at different sizes?
  • XSD with multi occurrences unordered
  • Jetty 9 HashLoginService
  • Custom validator control occupying space even though display set to dynamic
  • Jquery UI tool tip close icon
  • Read a local file using javascript
  • ImageMagick, replace semi-transparent white with opaque white
  • does jqgrid support a multiple checkbox list for editing
  • Cannot connect to cassandra from Spark
  • Optimizing database types to compact database (SQLite)
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • Resize panoramic image to fixed size
  • Volusion's generic SQL folder, functionality
  • Cross-Platform Protobuf Serialization
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • Do I've to free mysql result after storing it?
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Load html files in TinyMce
  • Free memory of cv::Mat loaded using FileStorage API
  • How do you join a server to an Active Directory (domain)?
  • coudnt use logback because of log4j
  • Turn off referential integrity in Derby? is it possible?