Passing array of strings from JS to C# inside WebBrowser control
I'm using ObjectForScripting property to interact with web page inside WebBrowser control and everything works fine except I can't figure out how to pass array of strings back to C#
HTML code
<input type="submit" onclick="window.external.save(Array('test', 'test2'))" />
Form
// Returns System.__ComObject
public void Save(object parameters)
{
}
// Throws an exception
public void Save(object[] parameters)
{
}
// Also thro开发者_如何学Cws an exception
public void Save(string[] parameters)
{
}
Rather than fight it; maybe approach the problem from another angle... could you (instead, either of):
- delimit the data (with
Array.join
) and pass a single string, and split it (string.Split
) in the C# - call Save multiple times, accepting a single string each time (
Save(string s)
), then call a final method to actually commit the changes
You can use an anonymous object instead of an array on the javascript side:
<input type="submit" onclick="window.external.save({first: 'test', second: 'test2'})" />
On the C# side (you need to use .NET 4.0 or more for the dynamic or use Type.InvokeMember if you are on an older version):
public void Save(dynamic parameters)
{
MessageBox.Show(parameters.first);
MessageBox.Show(parameters.second);
}
Not tested, but I think you can use reflection to discover the members.
Also look at this: http://dotnetacademy.blogspot.fr/2009/11/vbnetcnet-communication-with-javascript.html
function JS2VBArray( objJSArray )
{
var dictionary = new ActiveXObject( "Scripting.Dictionary" );
for ( var i = 0; i < objJSArray.length; i++ )
{
dictionary.add( i, objJSArray[ i ] );
}
return dictionary.Items();
}
Reference: http://msdn.microsoft.com/en-us/library/zsfww439(v=vs.80).aspx
<input type="submit" onclick="window.external.Save( JS2VBArray( ['test', 'test2'] ) )" />
This should go to the method.
public void Save(object[] parameters)
{
}
The string array is automatically passed as a comma-delimited string. So this call:
window.external.save(Array('test', 'test2'));
Is received like so:
public void save(string fromjs)
{
string[] result = fromjs.Split(',');
}
It's a bit late for this, but typically when I need to pass objects or, in this case Arrays, I pass them as a JSON string.
var sArr = JSON.stringify(myArr);
window.external(sArr);
Then I have a JavaScriptSerializer
on the other side that deserializes it back into an object / array.
Deserialize JSON with C#
To pass an array I found this to not be supported directly. I took the approach recommended by Marc Gravell to call multiple times but structured it in 3 methods instead, that are used in sequence: InitArgs
, PushArg
(multiple times), FinalArgs
.
private System.Collections.Generics.Queue<string> _argsQ;
public void InitArgs()
{
_argsQ = new System.Collections.Generics.Queue<string>();
}
public void PushArg(string arg)
{
_argsQ.Enqueue(arg);
}
public void FinalArgs()
{
string[] parameters = _argsQ.ToArray();
// Save parameters
}
Now method calls can be used sequentially from html/js:
...onclick="var params = ['test', 'test2']; window.external.InitArgs(); for (var i=0; i<params.length; i++) window.external.PushArg(params[i]); window.external.FinalArgs();"...
精彩评论