Tables created programmatically don't appear in WebBrowser control

Posted by John Hall on Stack Overflow See other posts from Stack Overflow or by John Hall
Published on 2009-11-19T17:01:41Z Indexed on 2010/03/15 22:59 UTC
Read the original article Hit count: 747

Filed under:
|
|

I'm creating HTML dynamically in a WebBrowser control. Most elements seems to appear correctly, with the exception of a table.

My code is:

var doc = webBrowser1.Document;
var body = webBrowser1.Document.Body;
body.AppendChild(webBrowser1.Document.CreateElement("hr"));

var div = doc.CreateElement("DIV");
var table = doc.CreateElement("TABLE");
var row1 = doc.CreateElement("TR");

var cell1 = doc.CreateElement("TD");
cell1.InnerText = "Cell 1";
row1.AppendChild(cell1);

var cell2 = doc.CreateElement("TD");
cell2.InnerText = "Cell 2";
row1.AppendChild(cell2);

table.AppendChild(row1);
div.AppendChild(table);
body.AppendChild(div);

body.AppendChild(webBrowser1.Document.CreateElement("hr"));

The HTML tags are visible in the OuterHTML property of the body, but all that appears in the browser are the two horizontal rules.

If I replace

div.AppendChild(table);

with

div.InnerHtml = table.OuterHtml

then everything appears as expected.

© Stack Overflow or respective owner

Related posts about c#

Related posts about dom