应用程序使用WebBrowser.DocumentText和DomDocument.DesignMode在IE8中工作并且在IE9中不起作用

时间:2011-08-22 04:25:53

标签: browser domdocument designmode

是的,我使用webBrowser控件在IE8中工作正常而在IE9中没有。似乎将HTMLDocument从DesignMode =“On”设置为DesignMode =“Off”会从WebBrowser中删除该文档。我做了这个显示我的问题的例子。表单上有两个按钮和一个webBrowser。一个按钮执行webBrowser.DocumentText,另一个按钮在document.DesignMode =“On”和“Off”之间切换。 DesignMode按钮使用“CheckOnClick”。我希望你能看到它的作用。

现在,如果我们在IE8的机器上运行它;然后切换进入和退出DesignMode确实将webBrowser.Document保留在原位。现在,如果我们在IE9的机器上运行它;然后将DesignMode设置为“On”或“Off”会导致webBrowser文档更改为“。如果webBrowser在DesignMode =”On“中,我们设置DocumentText;那么webBrowser现在处于DesignMode =”Off“。

我一直无法找到解决此问题的方法,以便能够在IE9中同时使用webBrowser.DocumentText和DesignMode。 IE8行为适用于我,而IE9则不适用。我无法想象如何设置DocumentText然后能够编辑它。

是否有设置或解决方法可以恢复IE8的行为?在IE9中对同一文档使用DocumentText和DesignMode似乎是不可能的。

提前感谢您的帮助。我花了很多时间不去寻找答案,到目前为止还没有。

    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>";
    }
}

我还尝试在WebBrowserDocumentCompleted事件中设置DesignMode,同样的问题(自动)发生。

        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);
    }

2 个答案:

答案 0 :(得分:3)

问题在于,当您设置DocumentText时,它会将designMode重置为“Inherit”,当您将designMode设置为“On”时,它会清除DocumentText。这似乎只发生在IE 9上。

此修复程序对我有用:

webBrowser1.Document.Body.SetAttribute("contentEditable", "true");

答案 1 :(得分:2)

感谢Eibrahim ..它似乎也适用于我的vb.net项目。我用它像

我把这段代码放在DocumentCompleted事件中,它运行良好Win7 + IE8和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