Solved

Stop AxWebBrowser navigation on Anchor click

Posted on 2008-06-17
5
763 Views
Last Modified: 2013-12-16
Hello Experts,
I want get the Anchor element href when it is clicked. With the attached code, I could get the href. But I dont want the Axwebbrowser to navigate to the href url. How can achieve thiis.

Thanks a billion


private void Form1_Load(object sender, EventArgs e)
        {
            
            object null_obj_str = "";
            System.Object null_obj = 0;
            axWebBrowser1.Navigate("about:blank", ref null_obj,ref null_obj, ref null_obj_str, ref null_obj_str);
            loadhtml();
        }
 
private void loadhtml()
        {
            IHTMLDocument2 myDoc = new HTMLDocumentClass();
            myDoc = (IHTMLDocument2)axWebBrowser1.Document;
            myDoc.write("<marquee WIDTH=100% BEHAVIOR=SCROLL DIRECTION=left scrollamount=2 BGColor=yellow onmouseover=stop(); onmouseout=start();><a href=http://www.raftaar.com/>akhtar</a> </marquee>");
            myDoc.close();
 
            HTMLDocumentEvents2_Event htmlEvents = (HTMLDocumentEvents2_Event)myDoc;
            htmlEvents.onclick += new HTMLDocumentEvents2_onclickEventHandler(htmlEvents_onclick);
        }
 
private bool htmlEvents_onclick(IHTMLEventObj pEvtObj)
        {
            if (pEvtObj.srcElement.tagName == "A")
            {
                HTMLAnchorElementClass a = pEvtObj.srcElement as HTMLAnchorElementClass;
                textBox1.Text = a.href;
            }
            return true;
        }

Open in new window

0
Comment
Question by:SHAHEEN_AKHTAR
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 13

Expert Comment

by:joechina
ID: 21815349
Have you tried to cancel the request in navigating event?

0
 

Author Comment

by:SHAHEEN_AKHTAR
ID: 21818511
I have tried onbeforenavigate event in which I called Axwebbrowser.Stop() method. This is also not working.

On downloadBegin event works fine for only first click. On second click, it doesnot work.

Can you kindly explain what you mean please.

Thanks a lot
0
 
LVL 13

Accepted Solution

by:
joechina earned 500 total points
ID: 21822245
Sorry, I am not so familiar with Axwebbrowser control.
.NET has a new WebBrowser control which has a Navigating event. In the event, you can set a flag to cancel a request.
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.aspx

From my experience, the new control could do almost everything as the old ActiveX control except the NewWindow2 event.
0
 

Author Closing Comment

by:SHAHEEN_AKHTAR
ID: 31468229
Thanks dear joechina. That is fine
0
 
LVL 13

Expert Comment

by:joechina
ID: 22008006
Hi, SHAHEEN_AKHTAR

Just let you know the new WebBrowser control is a wrap of the AxWebBrowser control. You can get a instance of the ActiveX control by:

SHDocVw.WebBrowser activeXBrowser = (SHDocVw.WebBrowser)webBrowser1.ActiveXInstance;

You can use all events and properties from both.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VS2010 Build fails to install 14 78
Regarding swagger API 1 35
VB.Net SQL Query 2 Tables Different Databases 3 43
orderby list (from Json) 1 29
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question