jQuery Ajax in ASP.Net

jQuery Ajax in ASP.NET

 
The following are asked many times on ASP.NET forums:
  1. How to call server-side methods or functions using JavaScript in ASP.NET
  2. How to call server-side methods client-side in ASP.NET
The answer is to use jQuery.
 
jQuery allows you to call server-side ASP.NET methods from the client-side without any PostBack. Actually, it is an AJAX call to the server but it allows us to call the method or function defined server-side.
 
The figure below describes the syntax of the call.
  1. $.ajax({  
  2.     type: "POST",  
  3.     url: "CS.aspx/MethodName",  
  4.     data: '{name: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',  
  5.     contentType: "application/json; charset=utf-8",  
  6.     dataType: "json",  
  7.     success: OnSuccess,  
  8.     failure: function(response) {  
  9.         alert(response.d);  
  10.     }  
  11. }); 
HTML Markup
  1. <div>  
  2.     Your Name :  
  3.     <asp:textbox id="txtUserName" runat="server"></asp:textbox>  
  4.     <input id="btnGetTime" type="button" value="Show Current Time" onclick="ShowCurrentTime()" />  
  5. </div>  
As you can see in the preceding I have added a TextBox when the user enters his name and a TML button that calls a JavaScript method to get the Current Time.
 
Client-side Methods
  1. <script src="scripts/jquery-1.3.2.min.js" type="text/javascript"></script>  
  2. <script type="text/javascript">  
  3.     function ShowCurrentTime() {  
  4.         $.ajax({  
  5.             type: "POST",  
  6.             url: "CS.aspx/GetCurrentTime",  
  7.             data: '{name: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',  
  8.             contentType: "application/json; charset=utf-8",  
  9.             dataType: "json",  
  10.             success: OnSuccess,  
  11.             failure: function (response) {  
  12.                 alert(response.d);  
  13.             }  
  14.         });  
  15.     }  
  16.     function OnSuccess(response) {  
  17.         alert(response.d);  
  18.     }  
  19. </script> 
The ShowCurrentTime method above makes an AJAX call to the server and executes the GetCurrentTime method that accepts the username and returns a string value.
 
Server-Side Methods
 
C#  
  1. [System.Web.Services.WebMethod]  
  2. public static string GetCurrentTime(string name)  
  3. {  
  4.     return "Hello " + name + Environment.NewLine + "The Current Time is: " +  
  5.         DateTime.Now.ToString();  
VB.Net  
  1. <System.Web.Services.WebMethod()> _  
  2. Public Shared Function GetCurrentTime(ByVal name As String) As String  
  3.    Return "Hello " & name & Environment.NewLine & "The Current Time is: " & _  
  4.             DateTime.Now.ToString()  
  5. End Function 
The preceding method simply returns a greeting message to the user along with the current server time. An important thing to note is that the method is declared as static (C#) or Shared (VB.Net) and also it is declared as a Web Method since unless you do this you won't be able to call the methods.


Recommended Free Ebook
Similar Articles