I want to call a C# method with parameter from JavaScript. It is possible, if I remove the parameter s
of the method <% showDetail(); %>
<% showDetail(); %>
function showDetail(kurz)
{
String s = kurz.toString();
<% showDetail(s); %>;
}
C# methods to test:
public void showDetail(String s)
{
Label_Test.Text = s.ToString();
}
public void showDetail()
{
Label_Test.Text = "";
}
It works fine without parameter but with s
variable I get a compiler error:
CS0103: The name 's' does not exist in the current context
I have tried
showDetail(Object s){....}
and also
showDetail(String s){....}
but it does not work.
Create a web method. That's an easy and neat way of calling c# methods from Javascript. You can call that method using jQuery Ajax. See the below example for a webMethod.
[WebMethod]
public static string RegisterUser(string s)
{
//do your stuff
return stringResult;
}
and then call this method using jQuery ajax. You can pass parameters also. like given 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
}
});
}
Use Hidden field to pass the value(set the value using javascript.). And call the javscript function with out parameter.. That value u can get it from the hidden field
You can achieve this by using WebMethods
First of all create a webmethod.
[WebMethod]
public string MethodName(string Parameter)
{
string msg=string.Empty;
//Your Code
return msg;
}
And in Java Script call that functions as
WebService.MethodName(Parameter,onSuccess,Error) // Here Webservice is the name of your asmx file
function onSuccess(result)
{
//Your code
}
function Error()
{
alert("Error");
}
the solution provide by Varun Paul is the one I have used and it works as long you correct 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,
Its possible to interact c# application
with javascrip
t, use jint.dll
for that
Jint - Javascript Interpreter for .NET
For Example
Following 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 have to create the object of your class inside the javascript and is possible to call ac# 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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.