I want to call a C# method with parameter from JavaScript. It is possible, if I remove the parameter s
of the method <% showDetail(); %>
我想用JavaScript中的参数调用C#方法。如果我删除方法<%showDetail()的参数s,则有可能; %>
function showDetail(kurz)
{
String s = kurz.toString();
<% showDetail(s); %>;
}
C# methods to test:
要测试的C#方法:
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:
没有参数它工作正常,但有s变量我得到编译器错误:
CS0103: The name 's' does not exist in the current context
CS0103:当前上下文中不存在名称''
I have tried
我努力了
showDetail(Object s){....}
and also
并且
showDetail(String s){....}
but it does not work.
但它不起作用。
5 个解决方案
#1
5
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.
创建一个Web方法。这是从Javascript调用c#方法的简单而简洁的方法。您可以使用jQuery Ajax调用该方法。有关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
然后使用jQuery ajax调用此方法。您也可以传递参数。如下所示
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
}
});
}
#2
0
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
使用隐藏字段传递值(使用javascript设置值)。并使用out参数调用javscript函数。该值可以从隐藏字段中获取
#3
0
You can achieve this by using WebMethods
您可以使用WebMethods实现此目的
First of all create a webmethod.
首先创建一个web方法。
[WebMethod]
public string MethodName(string Parameter)
{
string msg=string.Empty;
//Your Code
return msg;
}
And in Java Script call that functions as
并在Java脚本调用中起作用
WebService.MethodName(Parameter,onSuccess,Error) // Here Webservice is the name of your asmx file
function onSuccess(result)
{
//Your code
}
function Error()
{
alert("Error");
}
#4
0
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}",
Varun Paul提供的解决方案是我使用的解决方案,只要您纠正以下错误,它就可以正常工作:数据:“{s:sParam}”,
It should be written as: data: { s:sParam },
它应该写成:data:{s:sParam},
data is used to pass parameters to the C# method. Hope it helps. Thanks,
data用于将参数传递给C#方法。希望能帮助到你。谢谢,
#5
-1
Its possible to interact c# application
with javascrip
t, use jint.dll
for that
它可以与javascript交互c#应用程序,使用jint.dll
Jint - Javascript Interpreter for .NET
Jint - .NET的Javascript解释器
For Example
例如
Following are the java script functions
以下是java脚本函数
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 a c# method
所以在这种情况下你必须在javascript中创建你的类的对象,并且可以调用c#方法
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);
}
#1
5
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.
创建一个Web方法。这是从Javascript调用c#方法的简单而简洁的方法。您可以使用jQuery Ajax调用该方法。有关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
然后使用jQuery ajax调用此方法。您也可以传递参数。如下所示
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
}
});
}
#2
0
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
使用隐藏字段传递值(使用javascript设置值)。并使用out参数调用javscript函数。该值可以从隐藏字段中获取
#3
0
You can achieve this by using WebMethods
您可以使用WebMethods实现此目的
First of all create a webmethod.
首先创建一个web方法。
[WebMethod]
public string MethodName(string Parameter)
{
string msg=string.Empty;
//Your Code
return msg;
}
And in Java Script call that functions as
并在Java脚本调用中起作用
WebService.MethodName(Parameter,onSuccess,Error) // Here Webservice is the name of your asmx file
function onSuccess(result)
{
//Your code
}
function Error()
{
alert("Error");
}
#4
0
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}",
Varun Paul提供的解决方案是我使用的解决方案,只要您纠正以下错误,它就可以正常工作:数据:“{s:sParam}”,
It should be written as: data: { s:sParam },
它应该写成:data:{s:sParam},
data is used to pass parameters to the C# method. Hope it helps. Thanks,
data用于将参数传递给C#方法。希望能帮助到你。谢谢,
#5
-1
Its possible to interact c# application
with javascrip
t, use jint.dll
for that
它可以与javascript交互c#应用程序,使用jint.dll
Jint - Javascript Interpreter for .NET
Jint - .NET的Javascript解释器
For Example
例如
Following are the java script functions
以下是java脚本函数
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 a c# method
所以在这种情况下你必须在javascript中创建你的类的对象,并且可以调用c#方法
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);
}