HtmlTable, HtmlTableRow, HtmlTableCell - creating thead, tbody and tfoot

Here is how (below). All classes used are in System.Web.UI.WebControls.

        TableRow headerRow = new TableHeaderRow();
        TableRow row2 = new TableRow();
        TableRow row3 = new TableFooterRow();
        Table table = new Table();

        var cell1 = new TableCell();
        headerRow.TableSection = TableRowSection.TableHeader;
        cell1.Text = "Header";
        headerRow.Cells.Add(cell1);

        var cell2 = new TableCell();
        cell2.Text = "Contents";
        row2.Cells.Add(cell2);

        var cell3 = new TableCell();
        cell3.Text = "Footer";
        row3.Cells.Add(cell3);
        row3.TableSection = TableRowSection.TableFooter;


        table.Rows.Add(headerRow);
        table.Rows.Add(row2);
        table.Rows.Add(row3);
        this.Controls.Add(table);

Doesn't look good, I'm afraid. This from http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmltable%28VS.80%29.aspx

Note

A complex table model is not supported. You cannot have an HtmlTable control with nested < caption >, < col >, < colgroup >, < tbody >, < thead >, or < tfoot > elements. These elements are removed without warning and do not appear in the output HTML. An exception will be thrown if you attempt to programmatically add these table model elements to the Control.Controls collection of the HtmlTable control.

Old school html is the way, then.


You can use

HtmlGenericControl table = new HtmlGenericControl("table");

instead of

HtmlTable table = new HtmlTable();

Tags:

C#

Html