ASP.NET Code Behind Method is Call by jQuery Function and AJAX.
Let's See How
jQuery.ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="jQuery.aspx.cs"
Inherits="jQuery" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">
<title>Untitled Page</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"
type="text/javascript"></script> <script type ="text/javascript"> $(document).ready(function () { $('#<%=btnjQuery.ClientID %>').click(function () { $.ajax({ type: "POST", url: "jQuery.aspx/CodeBehindMethod", data: "{}", contentType: "application/json; charset=utf-8", dataType: "json", async: true, cache: false, success: function (msg) { $('#result').text(msg.d); } }) return false; }); }); </script> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="btnjQuery" runat="server" Text="call jQuery Function" /> <br /><br /> <div id="result"></div> </div> </form> </body> </html>
well do not forget System.Web.Services NameSpace and
Method Must be Static and it's Variable also Static. jQUery.aspx.cs
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)
{
}
[WebMethod]
public static string CodeBehindMethod()
{
return "Message from code behind Method.";
}
}