I have doubt regarding call .cs class file(C#) function from javascript My Code: I have class file(.cs) like call_cs_function_from_js
------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace call_cs_function_from_js
{
public class cal_lcs_function_from_js
{
public void getdata()
{
}
}
}
This is javascript code File:
<script type="text/javascript">
function call(){
cal_lcs_function_from_js.getdata(); //This way is not working
alert('called');
}
</script>
Here I want call getdata of cal_lcs_function_from_js from call()(means .js). call() invoked when button click.
Please Show me what are the other ways.
I have doubt regarding call .cs class file(C#) function from javascript My Code: I have class file(.cs) like call_cs_function_from_js
------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace call_cs_function_from_js
{
public class cal_lcs_function_from_js
{
public void getdata()
{
}
}
}
This is javascript code File:
<script type="text/javascript">
function call(){
cal_lcs_function_from_js.getdata(); //This way is not working
alert('called');
}
</script>
Here I want call getdata of cal_lcs_function_from_js from call()(means .js). call() invoked when button click.
Please Show me what are the other ways.
Share Improve this question edited May 21, 2012 at 6:36 Developer 2,00612 silver badges11 bronze badges asked May 21, 2012 at 6:27 user1402972user1402972 111 silver badge4 bronze badges 2- The code you've written wouldn't work in c# either. It's a method and as such you would need an instance of the class. – Rune FS Commented May 21, 2012 at 6:37
-
Off-topic and extremely nitpicky: apart from the fact that you're ignoring all case-conventions, why's your function named
getdata()
when it doesn't return anything? – Thorsten Dittmar Commented May 21, 2012 at 6:41
2 Answers
Reset to default 5You can not call your C# function directly from your javascript code. As javascript runs on client side and your C# function resides on the server.
For that you have to create a Web Service, and call that service form your javascript using Ajax
.
UPDATE:
- First add the namespace
using System.Web.Services;
to your web page. Add the following method to your page
[WebMethod] public string GetData() { return (""); }
Call the method using
Ajax
.$.ajax({ type: "GET", url: "/GetData", success: function (data) { });
The only way that you can call a C# function using javascript is if you're running the C# function within an ASP.NET page. Then you would use an ajax call from the javascript to call the ASP.Net page and retrieve the results of your function.
http://api.jquery./jQuery.ajax/
function call(){
$.ajax({
url: "/cal_lcs_function_from_js"
}).done(function() {
alert('called');
});
}
where "/cal_lcs_function_from_js" is a page running in ASP on the same web server as the javascript file is being run from.