使用参数从 JavaScript 调用 C# 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18610042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call C# method from JavaScript with parameter
提问by Butters
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# 方法。有可能,如果我删除s
方法的参数<% showDetail(); %>
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:当前上下文中不存在名称“s”
I have tried
我试过了
showDetail(Object s){....}
and also
并且
showDetail(String s){....}
but it does not work.
但它不起作用。
采纳答案by Varun Paul
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.
创建一个网络方法。这是一种从 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
}
});
}
回答by Sumeshk
Its possible to interact c# application
with javascrip
t, use jint.dll
for that
它可以c# application
与javascrip
t交互,jint.dll
用于那个
Jint - Javascript Interpreter for .NET
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 中创建你的类的对象,并且可以调用 ac# 方法
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);
}
回答by Vinay Kumar
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 函数.. 该值可以从隐藏字段中获取
回答by Vamsi Pamula
You can achieve this by using WebMethods
您可以通过使用 WebMethods 来实现这一点
First of all create a webmethod.
首先创建一个webmethod。
[WebMethod]
public string MethodName(string Parameter)
{
string msg=string.Empty;
//Your Code
return msg;
}
And in Java Script call that functions as
在 Java Script 调用中,它的作用是
WebService.MethodName(Parameter,onSuccess,Error) // Here Webservice is the name of your asmx file
function onSuccess(result)
{
//Your code
}
function Error()
{
alert("Error");
}
回答by alex leo
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# 方法。希望能帮助到你。谢谢,