简体   繁体   English

使用参数从JavaScript调用C#方法

[英]Call C# method from JavaScript with parameter

I want to call a C# method with parameter from JavaScript. 我想从JavaScript调用带有参数的C#方法。 It is possible, if I remove the parameter s of the method <% showDetail(); %> 如果我删除方法<% showDetail(); %>的参数s ,这是可能的<% showDetail(); %> <% 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. 但它不起作用。

Create a web method. 创建一个网络方法。 That's an easy and neat way of calling c# methods from Javascript. 这是从Javascript调用c#方法的简便方法。 You can call that method using jQuery Ajax. 您可以使用jQuery Ajax调用该方法。 See the below example for a webMethod. 请参见下面的示例以获取webMethod。

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

and then call this method using jQuery ajax. 然后使用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.). 使用“隐藏”字段传递值(使用javascript设置值)。 And call the javscript function with out parameter.. That value u can get it from the hidden field 并使用out参数调用javscript函数。该值u可以从隐藏字段中获取。

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 Script调用中,

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}", Varun Paul提供的解决方案是我使用过的解决方案,只要您纠正以下错误,它就可以起作用:data:“ {s:sParam}”,

It should be written as: data: { s:sParam }, 它应写为:数据:{s:sParam},

data is used to pass parameters to the C# method. 数据用于将参数传递给C#方法。 Hope it helps. 希望能帮助到你。 Thanks, 谢谢,

Its possible to interact c# application with javascrip t, use jint.dll for that 可以将c# applicationjavascrip t进行交互, jint.dll可以使用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 ac# 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM