簡體   English   中英

C#Web瀏覽器控件未正確更新

[英]C# Web browser control is not updating correctly

我目前正在開發一個與使用動態內容的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觸發之前立即執行)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM