Buzz
Buzz

Reputation: 6330

handle web browser control event in code behind

can we handle the event inside the web browser control? in my case i am passing a radio button list to web browser control and want to handle the radio button change event in code behind,
i try to google it but didnt find any concrete example.

 WebBrowser.NavigateToString("<html>
<body>
<input type='radio' name='g1' value='V1'> V1<br>
<input type='radio' name='g1' value='V2' V2<br>
</body>
</html>");

how can i get change event of these radio button change in codebehind ,i can use javascript but that donot fill my objective

can any one suggest how to do this.

Upvotes: 1

Views: 1874

Answers (1)

Erti-Chris Eelmaa
Erti-Chris Eelmaa

Reputation: 26268

Invoke C# method from Javascript

Communication between html document and WPF requires you to have full trust between the applications. In javascript, window.external points to the external application, which you might use to invoke a method outside the WebBrowser.

To do this you need to create an interface between the two. A helper method should be used to which could be accessed directly using Javascript. Let us look how we can achieve this using WPF WebBrowser Control.

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[ComVisible(true)]
public class ObjectForScriptingHelper
{
    Mainwindow mExternalWPF;
    public ObjectForScriptingHelper(Window1 w)
    {
        this.mExternalWPF = w;
    }

}

So basically the class allows you to invoke a .NET method directly from javascript. This helper class is set Permission to FullTrust and also ComVisible. So our WebBrowser, which is actually a Com element can directly communicate with the class to invoke method within the class ObjectForScriptingHelper, which is the parent window on which the browser is loaded. The javascript will allow to use window.external to point to this class.

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[ComVisible(true)]
public class ObjectForScriptingHelper
{
    Mainwindow mExternalWPF;
    public ObjectForScriptingHelper(Window1w)
    {
        this.mExternalWPF = w;
    }
    public void InvokeMeFromJavascript(string jsscript)
        {
            this.mExternalWPF.tbMessageFromBrowser.Text = string.Format("Message :{0}", jsscript);
        }

}

Say I have a method InvokeMeFromJavascript within the class ObjectForScriptingHelper class. To use this class you need to create an object of it and pass it to the property ObjectForScripting of WebBrowser control.

So I write,

ObjectForScriptingHelper helper = new ObjectForScriptingHelper(this);
this.wbMain.ObjectForScripting = helper;

Now Lets navigate to an html with :

<input type="text" id="txtMessage" />
<input type="button" value="InvokeMe" onclick="javascript:window.external.InvokeMeFromJavascript(document.getElementById('txtMessage').value);" />

This will load a textbox and a button. See in the code above, I have used window.external to call the same function that I have declared in the ObjectForScriptHelper class. Thus when you click on the Button inside the WebBrowser, you will see the message been displayed in the TextBlock outside it.

In the above image, when the user clicks on InvokeMe inside the WebBrowser, it will update the TextBlock placed outside.

Source: www.dotnetfunda.com/articles/article840-working-with-webbrowser-in-wpf.aspx

Upvotes: 4

Related Questions