使用WebBrowser.DocumentText和DomDocument.DesignMode的应用程序在IE8中有效,而在IE9中无效 [英] App Using WebBrowser.DocumentText And DomDocument.DesignMode Works In IE8 And Does Not Work In IE9

查看:134
本文介绍了使用WebBrowser.DocumentText和DomDocument.DesignMode的应用程序在IE8中有效,而在IE9中无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

现在,如果我们在装有IE8的计算机上运行此代码,则可以。然后切换进入和退出DesignMode确实会将webBrowser.Document保留在原位。现在,如果我们在装有IE9的计算机上运行它;然后将DesignMode设置为 On或 Off会使webBrowser文档更改为。如果webBrowser处于DesignMode = On,并且我们设置了DocumentText;则webBrowser现在处于DesignMode = Off。

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".

我无法找到解决此问题的方法,以便能够在IE9中同时使用webBrowser.DocumentText和DesignMode。IE8行为适用于我和IE9都没有。我无法想象如何设置DocumentText,然后再对其进行编辑。

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.

有设置或工作吗?可以使IE8的行为恢复原状吗?似乎不可能在IE9中的同一文档上使用DocumentText和DesignMode。

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

我也尝试在WebBrowserDocumentCompleted中设置DesignMode事件,并且会自动发生相同的问题。

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


推荐答案

问题是,当您设置DocumentText,则将designMode重置为继承,并且将designMode设置为 On时,它将清除DocumentText。这似乎仅在IE 9上发生。

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.

此修复程序对我有用:

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

这篇关于使用WebBrowser.DocumentText和DomDocument.DesignMode的应用程序在IE8中有效,而在IE9中无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆