使用WebBrowser控件作为编辑器的问题

时间:2010-05-03 21:08:18

标签: c# .net winforms webbrowser-control designmode

我目前正在开发一个项目,我将WebBrowser控件用作编辑器。我打开了设计模式,它似乎正在工作。我遇到的问题是,当我尝试保存文档并加载另一个文件时,会弹出“此文档已被修改”。信息。我想做的就是这个简单

if (frontPage)
{
    frontPage = false;
    frontContent = webEditor.DocumentText;
    webEditor.DocumentText = backContent;
}
else
{
    frontPage = true;
    backContent = webEditor.DocumentText;
    webEditor.DocumentText = frontContent;
}

就像我说每次输入一些文本并运行此代码时,它会弹出一条消息,说明它已被修改并询问我是否要保存。我怎么能绕过这个?

7 个答案:

答案 0 :(得分:2)

您应该创建以下功能:

void ChangeAllowWebBrowserDrop() { webBrowser.AllowWebBrowserDrop = !webBrowser.AllowWebBrowserDrop; }

每次更改DocumentText之前都应该调用它。

答案 1 :(得分:1)

你可以设置

BodyHtml.  

像这样:

string newHTMLString="some html";
webBrowser1.Document.Body.InnerHtml = newHTMLString;

为我工作。

答案 2 :(得分:1)

您应该创建以下功能:

void ChangeAllowWebBrowserDrop() {
    webBrowser.AllowWebBrowserDrop = !webBrowser.AllowWebBrowserDrop;
}

每次更改DocumentText之前都应该调用它。

答案 3 :(得分:0)

试试这个webEditor.ScriptErrorsSuppressed = true;

确保已在IE中禁用了“脚本调试”,以防您将其打开。

答案 4 :(得分:0)

Windows窗体加载文档流(由DocumentText属性使用)的方式是导航到about:blank,它触发文档修改的消息,然后在其DocumentComplete事件处理程序中加载流。

由于您已有文档,因此可以跳过导航并直接通过其IPersistStreamInit接口将流加载到现有文档中,就像Windows Forms在其DocumentComplete事件处理程序中所做的那样。

答案 5 :(得分:0)

更好的解决方案是在实际分配html代码之前写入空字符串: WebBrowser1.Document.Write(的String.Empty);
WebBrowser1.DocumentText =“您的代码”;

答案 6 :(得分:0)

我已经解决了这个问题:

browser.Document.Write(string.Empty);  
browser.DocumentText="Your html code"; 

这是来自此链接: http://social.msdn.microsoft.com/Forums/vstudio/en-US/3a9c1965-8559-4972-95e1-da0e86cf87bb/webbrowser-strange-problem

相关问题