如何在WebBrowser控件的表中插入新行 [英] How to insert a new row into a table in a WebBrowser control

查看:56
本文介绍了如何在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.

注意:根据 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控件的表中插入新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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