KF2
KF2

Reputation: 10143

How can I return data from JavaScript in a WebBrowser control to C#?

I have a WebBrowser control on my WinForm, within which I'm showing a dialog box with jQuery that contains a text area and a button labeled "save", the purpose of which is to obtain data from a user.

When the user clicks the save button I wish to retrieve the text in a text area and store it in a variable in my WinForm application.

Is this possible: jQuery function returning a value to a WinForm application?

Upvotes: 2

Views: 4262

Answers (2)

myermian
myermian

Reputation: 32505

Yes it is.

If you plan to use the standard WebBrowser control for WindowsForm see this property: WebBrowser.ObjectForScripting -- There is a very useful example at the bottom showing you exactly how you would execute Javascript that calls back into your application.

Personally, I don't like the supplied WebBrowser control for WinForms or WPF -- They are simply COM Wrappers for internet explorer. I prefer to use Awesomium.NET.

Upvotes: 3

akton
akton

Reputation: 14376

Do you specifically want to use jQuery to do this or do you just want to do bidirectional communication? Using jQuery or traditional web practices will require a web server running somewhere for the page to query.

If you just want bidirectional communication, set the WebBrowser.ObjectForScripting property. This exposes a .Net object to the JavaScript code on the web page as window.external. The JavaScript code on the page can then call methods and properties on that object to communication with your WinForms application, like storing the text from the text area when the Save button is pressed.

See http://msdn.microsoft.com/en-us/library/a0746166.aspx for more information.

Upvotes: 2

Related Questions