There are remarkable amount of ways to call a c# function from the jQuery Function.
But I like to share the way, which I have known before few days for my project. It is really so simple than my expectation.
There is something referred as jQuery.ajax() request in the Ajax API in the jQuery. This request Load a remote page using an HTTP request and returns the XMLHttpRequest that it creates.
The request implementation is in the following code snippet.
$.ajax({
type: "POST",
url:"MyPage.aspx/MyFunction",
cache: false,
data: "{'arg':'" +12 + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) { alert(“success”);
}
The type, url, cache and so on referred as “options”. To more options and more explanations, follow the link.
http://docs.jquery.com/Ajax/jQuery.ajax#options
Here, the given url “MyPage.aspx/MyFunction” means the “MyFunction” Web Method in the “MyPage.aspx” web page.
[WebMethod]
public static void MyFunction(int arg)
{
//Put your code here
}
Here, I have not used any Web Services. I just used a c# Web Method Which is in the
web page. For this kind of Page Web Method implementation, static methods should be
used.
No comments:
Post a Comment