This is a known bug. See http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q311298&ID=KB;EN-US;
If you are using the second solution from our FAQ ‘How can I host a WebBrowser control in a Windows Form’ you will not have this problem but if you use the automatically generated wrapper classes see the following solution:
John Cullen posted the following answer in the microsoft.public.dotnet.framework.interop newsgroup.
The problem of the BeforeNavigate2 event not firing in C# applications has been floating around various groups for several months. Microsoft have not yet fixed the problem, although it is documented in the knowledge base. Until they do provide a fix, I suggest the following workaround to the problem which uses the fact the the old Webbrowser_V1 BeforeNavigate event *can* be caught. Note, use of this interface is deprecated, however since there doesn’t appear to be any chance of a fix any time soon…
...
// define the webbrowser object
private AxSHDocVw.AxWebBrowser axDocument;
// define an IE3 compatible webbrowser object.
private SHDocVw.WebBrowser_V1 axDocumentV1;
public Form1()
{
//...
object o = null;
axDocument.Navigate('about:blank', ref o, ref o, ref o, ref o);
object oOcx = axDocument.GetOcx();
try
{
axDocumentV1 = oOcx as WebBrowser_V1;
axDocumentV1.BeforeNavigate += new SHDocVw.DWebBrowserEvents_BeforeNavigateEventHandler(this.axDocumentV1_BeforeNavigate);
}
catch (Exception ex)
{
// ignore errors. If it doesn’t work, there’s not a lot to do!
Console.WriteLine('Add BeforeNavigate event handler failed with{0}.', ex.Message);
}
//...
}
private void axDocumentV1_BeforeNavigate(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)
{
Console.WriteLine('BeforeNavigateURL= {0}', URL);
//false= allow navigate to continue.
//true= cancel navigation.
Processed=false;
}
Share with