How to Call C# server side Method from java script in with C# using Jquery Ajax and JSON

In previous example we had learn How to call jquery function from code behind in with C#.
and otherIn this Tutorial we will learn something new which is really helpful for dynamic web applications without loading. There are many situations where we need to do some action in Code behind(C#) without post back. In this type of situation generally we are using Update Panels of course this is suitable when you want to execute a specific control events you can register it with using triggers in Update Panel but when you want to call server side Method from java script then Jquery AJAX is the best approach. It is faster and easier. We can easily improve user experience and performance of web applications by reducing unnecessary post backs.

One of the best things which I like in jQuery is JQuery Ajax functions. this is a way through which we can call server side page's method in java script. This brings so many opportunities we can perform lots of operations without using slow and annoying post backs.

In this post I am showing the basic use of  Jquery AJAX. In this example I am creating a small application in which i am calling one Server side Method from java script with two arguments.

Here is the screening of below web application. Show this image carefully there is no loading sign in browser tab.

Add below Code to aspx page.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
<script src="">
    <script type="text/javascript" language="javascript">
$(document).ready(function () {
            $('#<% =Button1.ClientID %>').click(function () {
                      type: "POST",
                      url: "WebForm1.aspx/serverMethodHello",
                      data: JSON.stringify({ name: 'Abhay'}),
                      contentType: "application/json; charset=utf-8",
                      dataType: "json",
                      async: true,
                      cache: false,
                      success: function (msg) {
                      error: function (XMLHttpRequest, textStatus, errorThrown) {
                          var jsonError = JSON.parse(XMLHttpRequest.responseText);
                return false;


    <form id="form1" runat="server">
        <div class="log"></div>
        <div id="myDiv" style="font-size: x-large; color: saddlebrown; background-color: #C1C1C1">
        <br />
        <p>Click This Button To CALL Server Side Method</p>
        <asp:Button ID="Button1" runat="server" Text="Button" />


Code in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Services;

namespace WebApplication1
    public partial class WebForm1 : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)

        public static string serverMethodHello(string name)
            return ("Hello this is server message......."+name);

Post a Comment