在webbrowser对象中向现有表添加行

时间:2014-10-06 14:24:15

标签: html vb.net html-table webbrowser-control

我在webbrowser对象中加载了一个包含table-element的网站。表的id是“MyTable2”。我需要能够添加新行。每一行都是这样的:

<tr class="ms-rteTableOddRow-default" style="text-align:center">
     <th class="ms-rteTableFirstCol-default"></th>
     <td class="ms-rteTableOddCol-default"></td>
     <td class="ms-rteTableEvenCol-default">​</td>
     <td class="ms-rteTableOddCol-default">​</td>
     <td class="ms-rteTableEvenCol-default">​</td>
     <td class="ms-rteTableOddCol-default"></td>
     <td class="ms-rteTableEvenCol-default">​</td>
</tr>

我可以通过

获取table-element
Dim element As HtmlElement = WebBrowser1.Document.GetElementById("table2")

我认为可以使用element.AppendChild(row)添加它,但我不知道如何从我的行创建一个htmlelement,如何在我的webbrowser中显示新行?

1 个答案:

答案 0 :(得分:1)

我设法创建了一个解决方案:

Private Sub AddRowToTable2()
    Dim element As HtmlElement = WebBrowser1.Document.GetElementById("table2")
    Dim mTBody As HtmlElement = element.FirstChild
    Dim mTR As HtmlElement = WebBrowser1.Document.CreateElement("TR")
    Dim mTh As HtmlElement = WebBrowser1.Document.CreateElement("TH")
    Dim mTDOdd1 As HtmlElement = WebBrowser1.Document.CreateElement("TD")
    Dim mTDEven1 As HtmlElement = WebBrowser1.Document.CreateElement("TD")
    Dim mTDOdd2 As HtmlElement = WebBrowser1.Document.CreateElement("TD")
    Dim mTDEven2 As HtmlElement = WebBrowser1.Document.CreateElement("TD")
    Dim mTDOdd3 As HtmlElement = WebBrowser1.Document.CreateElement("TD")
    Dim mTDEven3 As HtmlElement = WebBrowser1.Document.CreateElement("TD")
    mTR.Style = "TEXT-ALIGN: center"
    'controle of het een even of oneven rij moet zijn
    If mTBody.Children.Count Mod 2 = 0 Then
        mTR.SetAttribute("class", "ms-rteTableEvenRow-default")
    Else
        mTR.SetAttribute("class", "ms-rteTableOddRow-default")
    End If
    mTh.SetAttribute("class", "ms-rteTableFirstCol-default")
    mTDEven1.SetAttribute("class", "ms-rteTableEvenCol-default")
    mTDOdd1.SetAttribute("class", "ms-rteTableOddCol-default")
    mTDEven2.SetAttribute("class", "ms-rteTableEvenCol-default")
    mTDOdd2.SetAttribute("class", "ms-rteTableOddCol-default")
    mTDEven3.SetAttribute("class", "ms-rteTableEvenCol-default")
    mTDOdd3.SetAttribute("class", "ms-rteTableOddCol-default")
    'mTh.InnerHtml = <th class="ms-rteTableFirstCol-default">netasq</th>
    'mTDOdd1.InnerHtml = <td class="ms-rteTableOddCol-default">1</td>
    'mTDEven1.InnerHtml = <td class="ms-rteTableEvenCol-default">2</td>
    mTR.AppendChild(mTh)
    mTR.AppendChild(mTDOdd1)
    mTR.AppendChild(mTDEven1)
    mTR.AppendChild(mTDOdd2)
    mTR.AppendChild(mTDEven2)
    mTR.AppendChild(mTDOdd3)
    mTR.AppendChild(mTDEven3)
    mTBody.AppendChild(mTR)
End Sub