tags:

views:

490

answers:

1

I've got the WebBrowser control to open links in my default browser like this:

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
    {
        if (e.Url.ToString() != "about:blank")
        {
            e.Cancel = true;
            System.Diagnostics.Process.Start(e.Url.ToString());
        }
    }

This works great but if I load a document that contains some IFrame elements those will be opened in the system browser as well (mostly embedded stuff, like Google Maps, Digg icons, etc..).

How do I keep iframes loading in the Webbrowser control and user clicked links in the system browser?

A: 

I've come to the conclusion that the .NET WebBrowser component is almost useless in this regard. I've tried to read WebBrowserNavigatingEventArgs.TargetFrameName but it will return the name attribute of the iframe element only if the HTML document has it. Otherwise it will spit out an empty "" string. Returning null on non-frame links would have been tons more useful.

So the only fix I've found for this is using the AxWebBrowser control, and specifically listening to the BeforeNavigate2 event. I haven't tested as much as I should have, but it appears that the "flags" property in DWebBrowserEvents2_BeforeNavigate2Event is set to 64 each time it's user triggered.

private void axWebBrowser1_BeforeNavigate2(object sender, AxSHDocVw.DWebBrowserEvents2_BeforeNavigate2Event e)
{
    // 64 means user triggered
    if ((int)e.flags == 64 && e.uRL.ToString() != "about:blank")
    {
     e.cancel = true;
     System.Diagnostics.Process.Start(e.uRL.ToString());
    }
}

MSDN docs says that flags is an IE7+ only parameter, so I have no idea what happens on IE6 machines...

Using Internet Explorer from .NET has some really valuable information regarding AxWebBrowser.

La-Brat
also to note, it's not necessary to use the COM control. You can easily expand WebBrowser to hook into the required events (in this case, BeforeNavigate2) as described at http://blogs.artinsoft.net/mrojas/archive/2008/09/18/newwindow2-events-in-the-c-webbrowsercontrol.aspx
La-Brat
finally got to test this on IE6 and what do you know? it works. Yay!
La-Brat