views:

205

answers:

3

I am trying to call a function from a Silverlight application. It should be a very simple task to do but so far I am not getting the result that I am looking for.

This is my Silverlight code:

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        HtmlPage.Window.Invoke("SayHello", new string[] { "Salut!" });
    }

And this is the JavaScript code :

   function SayHello(theid) {
        alert(eval(theid));
        var divStatusDiv = document.getElementById("divStatus");
        divStatusDiv.style.backgroundColor = "Red";
    }

The alert message always show "undefined" but when I press "OK" the colour of that DIV gets changed to Red as it should be.

Why am I getting "Undefined" all the time ?

+1  A: 

I'm not familiar with Silverlight, but if theid has value "Salut!" inside of SayHello, then you cannot eval it, since it is a string of text, not code. You should change the line alert(eval(theid)); to just alert(theid);.

Adam
I have tried this but it doesn't work. Undefined is what I get.
Raha
A: 

Use

alert(eval(theid.value));
CodeToGlory
Also try to avoid using eval for both performance and code injection implications.
CodeToGlory
I don't think eval is a good idea, but please note that the arg is passed as an array of strings.
Raha
+2  A: 

You need to create the json that can be passed properly instead of just passing along an array like that. You can simply return "Salut!" instead of new string[] { "Salut!" } or you can create the json array for the string array you have.

nyxtom
How can I create json array in C# ? I found this : http://stackoverflow.com/questions/1056121/how-to-create-json-string-in-c is this what I need to do ?
Raha
Its solved now, I created a jason message.
Raha