5

I want to call a C# method with parameter from JavaScript. It is possible, if I remove the parameter s of the method <% 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.

6
  • 2
    geekzilla.co.uk/View7B75C93E-C8C9-4576-972B-2C3138DFC671.htm Commented Sep 4, 2013 at 9:35
  • are you using a c# windows application? Commented Sep 4, 2013 at 9:37
  • Can you specify if you're using ASP.NET and whether it is WebForms or MVC? Commented Sep 4, 2013 at 9:40
  • Thank you guys I will try it and @sumeshk yes I'm usind c# windows application Commented Sep 4, 2013 at 9:41
  • I don't think your are writting a Windows Application with such tags <% %> Commented Sep 4, 2013 at 9:42

5 Answers 5

6

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
        } 
    }); 
} 
Sign up to request clarification or add additional context in comments.

3 Comments

I've tryed but it does not work, cound you please give me an full solution or tutorial?
@SigiAnonym I have editted my answer and added the jScript method. Please try this one
Would I be able to host it in IIS and call it from a browser? How do I create the WebMethod? What type of project is it? I would like to know how the whole process works from creating to testing, any docs? Thanks.
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

Comments

0

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");
}

Comments

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}",

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

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

Comments

-1

Its possible to interact c# application with javascript, 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 a 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);
}

4 Comments

This is for running JavaScript on the server. I don't think that's what the OP wants.
No, we can communicate our c# application with a javascript function using this dll,i used this in my application and able to communicate with the javascript, calls a javascript function from c# methods and vice versa
Even tough the question isn't exactly explicit about that, I am pretty sure the OP needs to do that over HTTP in an ASP.NET application. Jint won't help with that.
@xavier-poinas From comment i think he is using a c# windows application, and down voting i think is unnecessary "Thank you guys I will try it and sumeshk yes I'm usind c# windows application"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.