使用WebBrowser.DocumentText和DomDocument.DesignMode的应用程序在IE8中工作,在IE9中不工作

使用WebBrowser.DocumentText和DomDocument.DesignMode的应用程序在IE8中工作,在IE9中不工作,browser,domdocument,designmode,Browser,Domdocument,Designmode,是的,我对webBrowser控件的使用在IE8中运行良好,而不是在IE9中。似乎将HTMLDocument从DesignMode=“On”设置为DesignMode=“Off”会从WebBrowser中删除文档。我举了一个例子来说明我的问题。表单上有两个按钮和一个webBrowser。一个按钮不显示webBrowser.DocumentText,另一个按钮在document.DesignMode=“On”和“Off”之间切换。设计模式按钮使用“CheckOnClick”。我希望你能看到它的作用

是的,我对webBrowser控件的使用在IE8中运行良好,而不是在IE9中。似乎将HTMLDocument从DesignMode=“On”设置为DesignMode=“Off”会从WebBrowser中删除文档。我举了一个例子来说明我的问题。表单上有两个按钮和一个webBrowser。一个按钮不显示webBrowser.DocumentText,另一个按钮在document.DesignMode=“On”和“Off”之间切换。设计模式按钮使用“CheckOnClick”。我希望你能看到它的作用

现在如果我们在一台有IE8的机器上运行它;然后,在设计模式中进行切换和切换会使webBrowser.Document保持原位。现在如果我们在一台有IE9的机器上运行它;然后将DesignMode设置为“开”或“关”会导致webBrowser文档更改为“。如果webBrowser处于DesignMode=“开”,并且我们设置了DocumentText;则webBrowser现在处于DesignMode=“关”

我一直无法找到一种方法来解决此行为,以便能够在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>";
    }
}

问题是,当您设置DocumentText时,它会将designMode重置为“继承”,而当您将designMode设置为“打开”时,它会清除DocumentText。这似乎只发生在IE 9上

此修复对我有效:

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

谢谢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

如果我能放弃100个,你会得到它-我在整个互联网上寻找解决方案,这是第一个(也是唯一的一个?)成功了!太好了,老兄!我同意Acme,这是唯一有效的方法…我已经为此奋斗了好几天了。在这个网站上感谢某人的方法就是接受他/她的答案。你可以通过点击答案左边的复选框来做到这一点。谢谢你Ranjan!你的帮助也是如此。
    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