如何在WebBrowser控件的表中插入新行

编程入门 行业动态 更新时间:2024-10-10 09:24:37
本文介绍了如何在WebBrowser控件的表中插入新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已将以下内容添加到表单 Load 上的 WebBrowser 控件中:

I have added the following content to a WebBrowser control on form Load:

private void Form1_Load(object sender, EventArgs e) { addFirst(); } private void addfirst() { string html = "<!DOCTYPE html>"; html += "<html><head><title>NewPage</title>"; html += "<style>"; html += "body {font-family: Verdana, Tahoma, sans-serif; font-size: 12px;} .vtop {vertical-align: top;} table th:nth-child(1) {width: 20%;}"; html += "table th:nth-child(2) {width: 30%;} table th:nth-child(3) {width: 50%;}"; html += "</style>"; html += "</head>"; html += "<body>"; html += "<table border='0' id='tableresult'>"; html += "</table>"; html += "</body></html>"; webbrowse.DocumentText = html; }

然后在按钮的 Click 事件处理程序上创建一个新的 tr 和两个 td 元素:

Then on Click event handler of a button I want to create a new tr and two td elements:

var doc = webbrowse.Document; var elem = doc.GetElementById("tableresult"); var email = elem.DomElement; var tr = webbrowse.Document.CreateElement("tr"); var td1 = webbrowse.Document.CreateElement("td"); var td2 = webbrowse.Document.CreateElement("td"); tr.AppendChild(td1); tr.AppendChild(td2); elem.AppendChild(tr);

但是当我在 WeBrowser 控件中检查源代码时,看不到新的TR或TD.

But when I check the source in the WeBrowser control, I don't see the new TR or the TDs.

我该如何解决?

推荐答案

有点棘手,但是您已经快到了.

It's a bit tricky, but you are almost there.

无论是否显式创建了 tbody ,您都需要找到 table 的 tbody 元素并将其添加到该行.

You need to find tbody element of the table and append the row to that, no matter you have created tbody explicitly or not.

调试技巧::右键单击 WebBrowser 控件并选择 View Source ,它会显示初始的 DocumentText ,而不是修改的.如果要查看更新的文档文本,请观看 webBrowser1.Document.Body.InnerHtml .

Debug tip: When you right click on WebBrowser control and choose View Source, it shows the initial DocumentText, not the modified one. If you want to see the updated document text, watch webBrowser1.Document.Body.InnerHtml.

注意:根据文档,设置 DocumentText 就足够了;设置 DocumentText 时,它将导航到 about:blank ,然后根据您设置的文本加载文档内容.因此,发生了所有预期的事情,例如创建Document对象或引发导航",导航"或 DocumentComplete 事件.遵循源代码也显示相同的内容.

Note: According to the documentation, setting DocumentText should be enough; when you set DocumentText, it navigates to about:blank and then loads the document content based on the text which you have set. So all the expected things like creating Document object or raising Navigating, Navigated or DocumentComplete events happen. Following the source code also shows the same.

示例

private void Form1_Load(object sender, EventArgs e) { var html = @" <!DOCTYPE html> <html> <head><title>NewPage</title></head> <body> <table id=""tableresult""> <thead> <th>Column 1</th> <th>Column 2</th> </thead> </table> </body> </html>"; webBrowser1.DocumentText = html; } private void button1_Click(object sender, EventArgs e) { var tr = webBrowser1.Document.CreateElement("tr"); var td1 = webBrowser1.Document.CreateElement("td"); td1.InnerHtml = "value 1"; var td2 = webBrowser1.Document.CreateElement("td"); td2.InnerHtml = "value 2"; tr.AppendChild(td1); tr.AppendChild(td2); webBrowser1.Document.GetElementById("tableresult") .GetElementsByTagName("tbody")[0].AppendChild(tr); }

更多推荐

如何在WebBrowser控件的表中插入新行

本文发布于:2023-10-26 18:08:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530936.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   如何在   WebBrowser

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!