它可以完成,但它會需要你的一些工作。首先,通過window.external和invoke腳本,在Web瀏覽器控件和任何頁面之間傳遞數據的最簡單方法是通過window.external和invoke腳本。當然,你必須在你的用戶控件中設置objectforscripting屬性。
一旦你完成了,你可以通過原始類型從javacript到.net兩種方式自由發送數據。現在針對你的對象,可能值得你花時間將它序列化成一個JSON字符串,然後通過window.external發送它,然後反序列化到.NET端的對應對象。你可以使用System.Web.Script.Serialization的JavaScriptSerializer或者當然你也可以使用JSON.net到deserialize。
下面是從MSDN
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.ObjectForScripting = this;
webBrowser1.DocumentText =
"<html><head><script>" +
"function test(message) { alert(message); }" +
"</script></head><body><button " +
"onclick=\"window.external.Test('called from script code')\">" +
"call client code from script code</button>" +
"</body></html>";
}
public void Test(String message)
{
MessageBox.Show(message, "client code");
}
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Document.InvokeScript("test",
new String[] { "called from client code" });
}
採取的基本思路