C # method call from JavaScript with parameter

I want to call a C # method with a parameter from JavaScript. Perhaps if I remove the s parameter of the method <% showDetail(); %> <% showDetail(); %>

 function showDetail(kurz) { String s = kurz.toString(); <% showDetail(s); %>; } 

C # Methods:

 public void showDetail(String s) { Label_Test.Text = s.ToString(); } public void showDetail() { Label_Test.Text = ""; } 

It works fine without a parameter, but with the s variable, I get a compiler error:

CS0103: name 's' does not exist in current context

I tried

 showDetail(Object s){....} 

as well as

 showDetail(String s){....} 

but that will not work.

+4
source share
5 answers

Create a web method. This is a simple and neat way to call C # methods from Javascript. You can call this method using jQuery Ajax. See the example below for webMethod.

 [WebMethod] public static string RegisterUser(string s) { //do your stuff return stringResult; } 

and then call this method using jQuery ajax. You can also pass parameters. as indicated below

 function showDetail(kurz) { String sParam = kurz.toString(); $.ajax({ type: "POST", url: "PageName.aspx/MethodName", data: "{s:sParam}", // passing the parameter contentType: "application/json; charset=utf-8", dataType: "json", success: function(retValue) { // Do something with the return value from.Net method } }); } 
+5
source

Use the Hidden field to pass the value (set the value using javascript.). And call the javscript function with out parameter. This u value can get it from a hidden field

0
source

You can achieve this using WebMethods

First of all, create a web method.

  [WebMethod] public string MethodName(string Parameter) { string msg=string.Empty; //Your Code return msg; } 

And in Java Script, a call that works like

 WebService.MethodName(Parameter,onSuccess,Error) // Here Webservice is the name of your asmx file function onSuccess(result) { //Your code } function Error() { alert("Error"); } 
0
source

the solution provided by Varun Paul is the one I used and it works until you have fixed the following error: data: "{s: sParam}",

It should be written as: data: {s: sParam},

data is used to pass parameters to the C # method. Hope it helps. Thanks,

0
source

Maybe its c# application interaction with javascrip t, use jint.dll for this

Jint - Javascript Interpreter for .NET

Example

Listed below are the java script functions

 function reverse(my_str) { var sb = new jintTestApplication.Test();//jintTestApplication is the namespace name return sb.Test1(reverse2(my_str) ); } function reverse2(my_str) { var comStr=""; var result=""; var i=my_str.length; i=i-1; for (var x = i; x >=0; x--) { result= my_str.charAt(x); comStr+=result; } return comStr; } 

so in this case you need to create an object of your class inside javascript and you can call the C # method

  JintEngine engine = new JintEngine(); engine.Run(ReadJavaScript()); Console.WriteLine(engine.Run("reverse('Foooooo');")); public static string ReadJavaScript() { string allines = File.ReadAllText(@"[path]\test.js"); } public void Test1(string message) { MessageBox.show(message); } 
-one
source

Source: https://habr.com/ru/post/1500479/


All Articles