h0ch5tr4355
h0ch5tr4355

Reputation: 2192

How can I pass a JSON array to a javascript function in the C# WebBrowser Control?

I have following javascript function in my HTML document:

function jsFunction(string, jsonArray, string) { ... }

An example of jsonArray would be following:

[
{"name":"foo", "value":"21980"},
{"name":"bar", "value":"100"},
{"name":"foo", "value":"27492328"},
{"name":"bar", "value":"WEB21980001831"}
]

I followed the instructions from the post "Creating an JSON array in C#" in order to create a JSON Array object in C#.

From my Windows Form I should be able to call the JavaScript function like this:

 Object[] jsParams = new Object[3];
 jsParams[0] = (Object)"test";
 jsParams[1] = new
 {
     items = new[] {
         new {name = "foo" , value = "21980"}, 
         new {name = "bar" , value = "100"}, 
         new {name = "foo" , value = "27492328"}, 
         new {name = "bar" , value = "WEB21980001831"}
     }
 };
 jsParams[2] = (Object)"test";

 this.webBrowserCtl.Document.InvokeScript("jsFunction", jsParams);

However, it doesn't work. Did I forget something?

Upvotes: 0

Views: 1097

Answers (1)

Hasan Jafarov
Hasan Jafarov

Reputation: 628

jsFunction is 3 parameter.

function jsFunction(string, jsonArray, string) { ... }

you send 4 argument.

 jsParams[0] = (Object)"test";
 jsParams[1] = new
 {
    items = new[] {
        new {name = "foo" , value = "21980"}, 
        new {name = "bar" , value = "100"}, 
        new {name = "foo" , value = "27492328"}, 
        new {name = "bar" , value = "WEB21980001831"}
    }
 };
 jsParams[2] = (Object)"content";
 jsParams[3] = (Object)"test";

Delete this line.

 //jsParams[3] = (Object)"test";

Parse jsonArray and use in jsFunction.

var data = JSON.parse(jsonArray ); 

Upvotes: 2

Related Questions