向Webbrowser添加元素

时间:2014-11-14 09:15:05

标签: c# .net webbrowser-control

我有一张id =" table1"加载到我的webbrowser webbSPPagina中。我需要添加一行。使用此代码,我可以将行添加到表元素,但它不会显示在webbrowser上。我是否需要将某些内容更改为我的webbrowser?

HtmlElement element = webbSPPagina.Document.GetElementById("table1");
HtmlElement mTBody = element.FirstChild;
HtmlElement mTR = webbSPPagina.Document.CreateElement("TR");
HtmlElement mTD1 = webbSPPagina.Document.CreateElement("TD");
HtmlElement mTD2 = webbSPPagina.Document.CreateElement("TD");
HtmlElement mTD3 = webbSPPagina.Document.CreateElement("TD");

mTD1.Style = "VERTICAL-ALIGN: top";
mTD2.Style = "VERTICAL-ALIGN: top";
mTD3.Style = "VERTICAL-ALIGN: top";
mTD1.SetAttribute("class", "ms-rtetablecells");
mTD2.SetAttribute("class", "ms-rtetablecells");
mTD3.SetAttribute("class", "ms-rtetablecells");
mTD1.InnerText = "Teamviewer Id";
mTD2.SetAttribute("id", "TeamviewerId");
mTD3.SetAttribute("id", "TeamviewerIdExtra");

mTR.AppendChild(mTD1);
mTR.AppendChild(mTD2);
mTR.AppendChild(mTD3);
mTBody.AppendChild(mTR);

1 个答案:

答案 0 :(得分:0)

我尝试了你的代码,如果我按照以下方式执行它,它对我有用

 //Add table in the form load event
 private void Form1_Load(object sender, EventArgs e)
 {
     webbSPPagina.DocumentText = "<table id='table1'><tr><td>hello</td></tr></table>";
 }

 //Added your code in a button click event
 private void button1_Click(object sender, EventArgs e)
 {
   HtmlElement element = webbSPPagina.Document.GetElementById("table1");
   HtmlElement mTBody = element.FirstChild;
   HtmlElement mTR = webbSPPagina.Document.CreateElement("tr");
   HtmlElement mTD1 = webbSPPagina.Document.CreateElement("td");
   HtmlElement mTD2 = webbSPPagina.Document.CreateElement("td");
   HtmlElement mTD3 = webbSPPagina.Document.CreateElement("td");

   mTD1.Style = "VERTICAL-ALIGN: top";
   mTD2.Style = "VERTICAL-ALIGN: top";
   mTD3.Style = "VERTICAL-ALIGN: top";
   mTD1.SetAttribute("class", "ms-rtetablecells");
   mTD2.SetAttribute("class", "ms-rtetablecells");
   mTD3.SetAttribute("class", "ms-rtetablecells");
   mTD1.InnerText = "Teamviewer Id";
   mTD2.SetAttribute("id", "TeamviewerId");
   mTD3.SetAttribute("id", "TeamviewerIdExtra");

   mTR.AppendChild(mTD1);
   mTR.AppendChild(mTD2);
   mTR.AppendChild(mTD3);
   mTBody.AppendChild(mTR);

  }