I have two projects Project1, Project2 in the same solution.
Project1 consists of a C# class "Sample.cs" and the class contains a method "TestMethod(count)".
Project2 consists of a javascript file "SampleScript.js" and it contains a function "ScriptFunction()".
I need to call the c# method TestMethod() from the js function.
Kindly let me know whether the above can be achieved without using ActiveX.
What methods can be used to achieve the above? Can the above be achieved using jquery?
KokilaRam
Member
1 Points
4 Posts
How to call c# method in javscript function
Jan 22, 2013 08:19 AM|LINK
Hi,
I have two projects Project1, Project2 in the same solution.
Project1 consists of a C# class "Sample.cs" and the class contains a method "TestMethod(count)".
Project2 consists of a javascript file "SampleScript.js" and it contains a function "ScriptFunction()".
I need to call the c# method TestMethod() from the js function.
Kindly let me know whether the above can be achieved without using ActiveX.
What methods can be used to achieve the above? Can the above be achieved using jquery?
Thanks in advance,
KokilaRam
AidyF
Star
9184 Points
1570 Posts
Re: How to call c# method in javscript function
Jan 22, 2013 08:26 AM|LINK
You can use PageMethods which is a built-in Microsoft library, or use the ajax features of jQuery. Both methods are detailed here
http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/
KokilaRam
Member
1 Points
4 Posts
Re: How to call c# method in javscript function
Jan 23, 2013 12:01 PM|LINK
Hi AidyF,
Thanks for the link. It was informative.
In my case, the I do not have aspx page, its only .cs file.
Can I still use the JQuery approach?
Thanks in advance,
Kokila
fayaz_3e
Star
9332 Points
1744 Posts
Re: How to call c# method in javscript function
Jan 23, 2013 12:07 PM|LINK
Yep. There are many other ways. I love to use HttpHanlders. You can try them.
http://lmgtfy.com/?q=jquery+ajax+and+httphandler
AidyF
Star
9184 Points
1570 Posts
Re: How to call c# method in javscript function
Jan 23, 2013 01:03 PM|LINK
Is your .cs code rendering the html? If so, as long as it renders the correct jQuery javascript (and script include) then it will still work.
l.laxmikant
Member
23 Points
23 Posts
Re: How to call c# method in javscript function
Jan 23, 2013 04:10 PM|LINK
You may do it by
1. Add a reference of project1 to project 2
2. Create a protected method in project2 and call method of project1.
3. Call Project2 protected method from js file using directlty calling or writing page methods or writing code with $.ajax
erabhinaw
Member
20 Points
6 Posts
Re: How to call c# method in javscript function
Jan 25, 2013 10:50 AM|LINK
you may call like this after adding reference of project 1 inside project 2
$(function() {
$.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: '10',
dataType: 'json',
url: 'sample.cs/testmethod',
success: function(result) {
BindData(result.d);
}
}); });