问题
Yes my use of webBrowser control work fine in IE8 and not in IE9. It seems that setting the HTMLDocument from DesignMode = "On" to DesignMode = "Off" removes the document from the WebBrowser. I made this example that shows my problem. There are two buttons and one webBrowser on a form. One button does the webBrowser.DocumentText and the other button toggles between document.DesignMode = "On" and "Off". The DesignMode button uses "CheckOnClick". I hope that you can see what it does.
Now if we run this on a machine with IE8; then toggling in and out of DesignMode does leave the webBrowser.Document in place. Now if we run this on a machine with IE9; then setting DesignMode to "On" or to "Off" causes the webBrowser document to change to ". If the webBrowser is in DesignMode = "On", and we set the DocumentText; then the webBrowser is now in DesignMode = "Off".
I have been unable to find a way to work around this behavior to be able to use webBrowser.DocumentText and DesignMode at the same time in IE9. The IE8 behavior works for me and the IE9 does not. I am unable to imagine how I might be able to set the DocumentText and then be able to edit it.
Is there a setting or work around to get the IE8 behavior back? It seems imposible to be able to use DocumentText and DesignMode on the same document in IE9.
Thanks in advance for any help. I have taken a lot of time to search no my own to find an answer, and have been unable so far.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
webBrowser1.DocumentText = "<HTML><BODY>Initial text</BODY></HTML>";
}
private void designModeToolStripButton_Click(object sender, EventArgs e)
{
if (this.designModeToolStripButton.Checked)
webBrowser1.Document.DomDocument.GetType().GetProperty("designMode").SetValue(webBrowser1.Document.DomDocument, "On", null);
else
webBrowser1.Document.DomDocument.GetType().GetProperty("designMode").SetValue(webBrowser1.Document.DomDocument, "Off", null);
}
private void setTextToolStripButton_Click(object sender, EventArgs e)
{
webBrowser1.DocumentText = "<HTML><BODY>New text</BODY></HTML>";
}
}
I also tried doing the setting of DesignMode in the WebBrowserDocumentCompleted event, and the same problem happens (automatically).
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (this.designModeToolStripButton.Checked)
webBrowser1.Document.DomDocument.GetType().GetProperty("designMode").SetValue(webBrowser1.Document.DomDocument, "On", null);
else
webBrowser1.Document.DomDocument.GetType().GetProperty("designMode").SetValue(webBrowser1.Document.DomDocument, "Off", null);
}
回答1:
The problem is that when you set DocumentText, it resets designMode to "Inherit" and when you set designMode to "On", it clears DocumentText. This seems to only happen on IE 9.
This fix worked for me:
webBrowser1.Document.Body.SetAttribute("contentEditable", "true");
回答2:
Thanks Eibrahim.. it seems to work for me as well in my vb.net project. I used it like
I put this code in DocumentCompleted event and it worked well Win7+IE8 and Win7+IE9
Try
If WebBrowser1.Document IsNot Nothing AndAlso WebBrowser1.Document.Body IsNot Nothing Then
WebBrowser1.Document.Body.SetAttribute("contentEditable", "true")
End If
Catch ex As Exception
DumpError(ex)
End Try
来源:https://stackoverflow.com/questions/7143167/app-using-webbrowser-documenttext-and-domdocument-designmode-works-in-ie8-and-do