loudit

February 23, 2009

How to call c# function from the jQuery function

Filed under: web development — mnalin @ 5:26 pm
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.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: