If you find this article useful, consider making a small donation to show your support for this web site and its content.
DiscountASP
AboutMe
About me:
Hi. My name is Farooq Kaiser and I'm a software developer from Toronto, Canada.

AJAX using jquery and ASP.NET

by Farooq Kaiser 7. February 2009 04:14

jQuery is a lightweight JavaScript library and can be downloaded from http://www.jquery.com

I’ll show you how to use jQuery to call a page method without using the ScriptManager. 

This will be our page method in ASP.NET code behind. 
public partial class _Default : Page 
{
  [WebMethod]
  public static string GetData()
  {
    return String.Format("Sever time is..{0}", DateTime.Now.ToString());
  } 


 Here is our Default.aspx

<html>
<head>
  <title>Demo with jQuery</title>
  <script type="text/javascript" src="jquery-1.2.6.min.js"></script>
  <script type="text/javascript" src="Demo.js"></script>
</head>
<body>
  <div id="divResult">Click here to get server time</div>
</body> 
</html> 

 

You can increase the performance by replacing above Script with google AJAX Libraries.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>

 Google AJAX Libraries can be found here 
http://code.google.com/apis/ajaxlibs/ 


Here is our Demo.js

$(document).ready(function() {
  $("#divResult").click(function() {
    $.ajax({
      type: "POST",
      url: "Default.aspx/GetData",
      data: "{}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(res) {
        $("#divResult").text(res.d);
      }
});
});
 }); 

Currently rated 4.0 by 2 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

.NET | asp.net | C#

Comments

Jobs Autos Real estate Videos Power by Google