views:

51

answers:

0

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'&gt;Star Trek Production history</a>";
        html += "Go to <a href='http://www.google.com'&gt;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();
    }
}