Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a C# function from JavaScript?

I want to call CsharpFunction, a C# function in code-behind, from JavaScript. I tried the code below but whether the JavaScript condition is True or False, CsharpFunction was called regardless!

JavaScript code:

if (Javascriptcondition > 0) {    <%CsharpFunction();%> } 

C# code behind:

protected void CsharpFunction() {   // Notification.show(); } 

How do I call a C# function from JavaScript?

like image 227
IamNumber5 Avatar asked Aug 26 '13 09:08

IamNumber5


People also ask

How do you call C in C++?

Just declare the C++ function extern "C" (in your C++ code) and call it (from your C or C++ code). For example: // C++ code: extern "C" void f(int);

How do you call C from R?

The most basic method for calling C code from R is to use the . C() function described in the System and foreign language interfaces section of the Writing R Extensions manual. Other methods exist including the . Call() and .

What is the correct way to call a function?

You call the function by typing its name and putting a value in parentheses. This value is sent to the function's parameter. e.g. We call the function firstFunction(“string as it's shown.”);


2 Answers

You can use a Web Method and Ajax:

<script type="text/javascript">             //Default.aspx    function DeleteKartItems() {               $.ajax({          type: "POST",          url: 'Default.aspx/DeleteItem',          data: "",          contentType: "application/json; charset=utf-8",          dataType: "json",          success: function (msg) {              $("#divResult").html("success");          },          error: function (e) {              $("#divResult").html("Something Wrong.");          }      });    } </script>  [WebMethod]                                 //Default.aspx.cs public static void DeleteItem() {     //Your Logic } 
like image 186
user3098137 Avatar answered Sep 19 '22 20:09

user3098137


.CS File         namespace Csharp     {       public void CsharpFunction()       {         //Code;       }     }      JS code:     function JSFunction() {             <%#ProjectName.Csharp.CsharpFunction()%> ;     } 

Note :in JS Function when call your CS page function.... first name of project then name of name space of CS page then function name

like image 42
Vijay Mungara Avatar answered Sep 19 '22 20:09

Vijay Mungara