Sunday, July 17, 2011

calling method from jquery

ASP.NET Code Behind Method is Call by jQuery Function and AJAX.
Let's See How
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="jQuery.aspx.cs"
Inherits="jQuery" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html xmlns="">
<head runat="server">
<title>Untitled Page</title>
 <script src=""

      <script type ="text/javascript">

          $(document).ready(function () {

              $('#<%=btnjQuery.ClientID %>').click(function () {


                      type: "POST",

                      url: "jQuery.aspx/CodeBehindMethod",

                      data: "{}",

                      contentType: "application/json; charset=utf-8",

                     dataType: "json",

                     async: true,

                     cache: false,

                     success: function (msg) {




                 return false;



    <form id="form1" runat="server">
    <asp:Button ID="btnjQuery" runat="server" Text="call jQuery Function" />

    <br /><br />

    <div id="result"></div>
well do not forget System.Web.Services NameSpace and
Method Must be Static and it's Variable also Static.
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Services;

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



    public static string CodeBehindMethod()


           return "Message from code behind Method.";