开发者

Problems using webbrowser control with ie7 and anchors

I'm seeing some very strange behavior when using a c# webbrowser control and a link with anchors in it. Oh and this behavior only seems to occur in ie7 in开发者_开发百科 ie8 it's fine!

I've created a simple form with a webbrowser control.

I've added a new WebBrowserNavigatedEventHandler to load in some html after the browser has loaded the first page. A straight link to Google works fine, but a link to a wikipedia page and anchor point does nothing.

If however I navigate to a page with anchors in it (or links to another page with links it works fine)!

So the issues may be with the page I'm loading the first time/how I'm loading it.

Here is the code I've written, any suggestions would be appreciated.

Edit:

I've just noticed that if I change the line

this.webBrowser1.Document.Write(html);

to:

this.webBrowser1.DocumentText = html;

it works fine!!!

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        this.webBrowser1.Navigated += new WebBrowserNavigatedEventHandler(webBrowser1_NavigatedLoaddefaultpage);
        this.webBrowser1.Navigated += new WebBrowserNavigatedEventHandler(webBrowser1_NavigatedUpdateTextbox);

        this.webBrowser1.Navigate("about:blank");
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.webBrowser1.Navigate(this.textBox1.Text);
    }


    void webBrowser1_NavigatedLoaddefaultpage(object sender, WebBrowserNavigatedEventArgs e)
    {
        this.webBrowser1.Navigated -= new WebBrowserNavigatedEventHandler(webBrowser1_NavigatedLoaddefaultpage);

        string html = "<html>";
        html += "<body>";

        html += "<h1>My First Heading</h1>";

        html += "<p>My first paragraph.</p>";

        html += "<a href='http://en.wikipedia.org/wiki/Star_Trek#Production_history'>Star Trek Production history</a>";
        html += "Go to <a href='http://www.google.com'>Google Search</a><br />";
        html += "</body>";
        html += "</html>";
        this.webBrowser1.Document.Write(html);
    }

    void webBrowser1_NavigatedUpdateTextbox(object sender, WebBrowserNavigatedEventArgs e)
    {
        this.textBox1.Text = this.webBrowser1.Url.ToString();
    }
}


This may not even be a problem with your code. It could be an anchor bug that IE7 has. I've been having issues with anchors not working in IE7 as well. Apparently sometimes this happens and sometimes it doesn't.


I seem to have just run into this issue as well. The webbrowser control refuses to load when I have a URL that includes an anchor (e.g., http://cnn.com/#foo). I'd swear this was working but I think Microsoft must have re-introduced this bug, assuming they had previously fixed it (or perhaps my test system just hadn't gotten the updates that included this bug).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜