Solved

Stop AxWebBrowser navigation on Anchor click

Posted on 2008-06-17
5
759 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now