C# Web 浏览器控件未正确更新

标签 c# javascript

我目前正在开发一个应用程序,该应用程序在技术上与使用动态内容的 html 页面进行交互。

我的问题是,当我尝试将数据附加到 WBC 时,内容未正确更新。

namespace CheckList
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        .... code removed ....

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != null)
            {
                HtmlDocument doc = webBrowser1.Document;
                HtmlElement row = doc.CreateElement("tr");
                HtmlElement cell1 = doc.CreateElement("td");
                HtmlElement cell2 = doc.CreateElement("td");
                cell1.InnerHtml = "[X] " + textBox1.Text;
                cell2.SetAttribute("class", "solved_2");
                cell2.InnerHtml = "Unsolved";
                row.AppendChild(cell1);
                row.AppendChild(cell2);
                doc.GetElementsByTagName("table")[0].AppendChild(row);
                //doc.Write(doc.GetElementsByTagName("HTML")[0].OuterHtml);
                webBrowser1.Document.Body.InnerHtml = doc.Body.InnerHtml;
            }
        }
    }
}

当前发生的情况是,我单击“添加”,它应该将 html 添加到页面并更新和 javascript,以及不应该加载的内容。

发生的情况是它添加了内容,但在我尝试重新加载内容后 JavaScript 不起作用。但 CSS 保持不变,并且 javascript 在该点之后就无法工作。

JS 来源:

var showalert = true;
var file = "file:///C:/Users/Removed/Documents/Visual Studio 2010/Projects/CheckList/CheckList/bin/Release/";
initiate_instance();

function initiate_instance() {
 //insert
 $.get(file + "saved.html", function(data) {
  //$("table#items").append("<tr><th width='70%'>Issue</th><th width='30%' class='right'>Solved</th></tr>");
  $("table#items").html($("table#items").html() + data);
 });

 //change [X] into a link
 $("table#items tr td").each(function() {
  $(this).html($(this).html().replace("[X]", "<a onclick='return remove(this)' href='#'>[X]</a>"));
 });

 //change the css
 $("table#items tr:odd").attr("class", "odd");
 $("table#items tr td:eq(0)").attr("width", "70%");
 $("table#items tr td:eq(1)").attr("width", "30%");
 $("td.solved, td.solved_2").click(function() {
  if($(this).attr("class") == "solved") {
   $(this).attr("class", "solved_2");
   $(this).text("Unsolved");
  } else {
   $(this).attr("class", "solved");
   $(this).text("Solved");
  }

  if(showalert == true) {
   alert("Remember, for these changes to keep effect please save before closing the program.");
   showalert = false;
  }
 });
}

//delete rows
function remove(obj) {
 if(showalert == true) {
  alert("Remember, for these changes to keep effect please save before closing the program.");
  showalert = false;
 }
 $(obj).parent().parent().remove();
 return false;
}

最佳答案

TL;DR:您是否尝试过将“AllowNavigation”设置为 true?

如果您需要阻止导航,但仍需要更新页面,我发现有效的方法需要:

  • 使用空 HTML 初始化 WebBrowser 控件的 DocumentText 属性以初始化内部对象(即:DocumentDomDocumentDocument.Body 等) )
  • 允许导航并在页面完成时撤销(如果需要)

代码:

namespace CheckList
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            // Initialize all the document objects
            webBrowser1.DocumentText = @"<html></html>";
            // Add the Document Completed event handler to turn off navigation
            webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            // Load default information via LoadHtml(string html);
            LoadHtml(@"<html><head></head><body>Text!<script type='text/javascript' language='javascript'>alert('Aha!');</script></body></html>");
        }

        private void LoadHtml(string html)
        {
            webBrowser1.AllowNavigation = true;

            // This will trigger a Document Completed event
            webBrowser1.DocumentText = html;
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            // Prevent further navigation
            webBrowser1.AllowNavigation = false;

            // Clean-up the handler if no longer needed
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // Do your document building
            LoadHtml(doc.Body.Parent.OuterHtml);
        }
    }
}

我发现这样做是这样的:

  • 在允许之前阻止用户导航
  • 允许执行 JavaScript(在 OnDocumentCompleted 触发之前)

关于C# Web 浏览器控件未正确更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4377954/

相关文章:

javascript - 正则表达式以特定字符结尾但忽略它

c# - Doxygen:HTML 太大

c# - 列出网络位置的所有共享文件夹

c# - 为类变量赋值

C# 列表,从对象列表创建列表对象

javascript - 按钮 - 仅当 JavaScript 函数返回 true 时才进行回发

javascript - 如何让第 n 个子选择器跳过隐藏的 div

javascript - 真的很困惑 re : how to use Parse Promises correctly in Cloud Code

javascript - "Unexpected token ."JSON 解析无法解析带前导零的十进制

c# - var 默认为哪种 int 类型?