Skip to content Skip to sidebar Skip to footer

Call .cs Class File(c#) Function From Javascript

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 --------------------------------------

Solution 1:

You 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:

  1. First add the namespace using System.Web.Services; to your web page.
  2. Add the following method to your page

    [WebMethod]
    publicstringGetData()
    {
        return ("");
    }
    
  3. Call the method using Ajax.

    $.ajax({ type: "GET", url: "/GetData", success: function (data) { });

Solution 2:

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.com/jQuery.ajax/

functioncall(){
  $.ajax({
    url: "/cal_lcs_function_from_js"
  }).done(function() { 
    alert('called');
  });
}

where "/cal_lcs_function_from_js" is a page running in ASP.net on the same web server as the javascript file is being run from.

Post a Comment for "Call .cs Class File(c#) Function From Javascript"