0

I'm working on creation of a winforms application which contains a WebBrowser user control. The WebBrowser control navigates to a page in a web application which requests user authentication details. When the user name and password are input and the form submitted, the web application sends a request and, if authentication is successful, populates variables in an applicationValues object.

When the applicationValues object is instantiated, I want to read the object from the web application in the WebBrowser control back into my Winforms application and close the form containing the WebBrowser control.

Currently, this is working (the object gets created successfully in the web application) until the part where the applicationValues object should get passed back to the Winforms application; there I'm stuck.

Can this be done with this control? If not, are there any other approaches or workarounds I should be considering?

1 Answer 1

1

It can be done, but it will require some work on your part. First of all, the easiest way to pass data back and forth between the web browser control and whatever page is in it, is via window.external and invoke script. Of course you have to set the objectforscripting attribute in your user control.

Once you have done that you can freely send data 2 ways from javacript to .net via primitive types. Now for your object specifically, it may be worth your time to serialize it into a JSON string and then send it via window.external, and then deserialize into a counterpart object on the .NET side. You could use System.Web.Script.Serialization's JavaScriptSerializer Or of course you could use JSON.net to deserialize as well.

Here is the basic idea taken from the 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" }); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.