tags:

views:

6

answers:

0

I have a WinForms 2.0 WebBrowser control that shows some dynamic HTML page, and I need to use some custom context menu for that.

In my page, I hove some links that execute javascript code, written with this markup:

<a href="#" onclick="myCode(); return false;">Click here</a>

Everything works fine, myCode() is called, and the href is not handled, after the return false. But when I register ANY C# event on the WebBrowser control, like:

webBrowser.Document.ContextMenuShowing += Document_ContextMenuShowing

Then, after myCode() is executed, the # href is used, and written in the document location. On some HTML pages (which set a tag), this actually navigates away from the page. It happens even if the ContextMenuShowing body is empty.

I already have some options to overcome the issue:

  • Change href="#" to href="javascript:void;" (but I don't like it, and I don't want to actually change my page, since they don't have any problem on their own)
  • Use another method (calling Win32 APIS, or something else) to handle the Context Menu

But I'd really like to know why this is happening, and if someone knows how to avoid it.

Thanks in advance