8

I'm working with c# ASP .Net, HtmlTable, HtmlTableRow, HtmlTableCell to create a table.

for example... i need to have some cells to be wrapped by <tfoot> and </tfoot>

i tried to do it with HtmlGenericControl to wrap these cells with the tag "tfoot" to the HtmlTable but it didn't work.

i know that HtmlTableCell can have in the constructor "th" for "thead" and "td" for tbody. but i need them to be wrapped with the actual "thead" and "tbody".

Any Suggestions??

Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
Jack
  • 667
  • 3
  • 9
  • 13

4 Answers4

22

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);
Ash
  • 2,575
  • 2
  • 19
  • 27
JR Kincaid
  • 823
  • 1
  • 7
  • 18
  • very very good. it help me. but they fo it only for header... there is no TableFooterCell... – Jack Jan 12 '10 at 20:41
  • 1
    Not necessary. You just make the row's TableSection value 'TableFooter'. These are the classes Gridview uses. – JR Kincaid Jan 12 '10 at 22:03
  • 1
    Keep in mind that the order is thead?, tfoot?, tbody+. The footer doesn't come at the end. – brianary Jan 12 '10 at 22:25
  • How can you have thead and tbody and use HTML code inside the cells? – Martin Mar 01 '11 at 09:58
  • The code above wasn't working for me. The tags inside the `` section where always `` instead of ``. It was driving me insane..until I found `New TableHeaderCell()` for the cells in the TableHeader. Now it's rendering properly! – weilah Sep 07 '16 at 14:01
4

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.

Community
  • 1
  • 1
graphicdivine
  • 10,937
  • 7
  • 33
  • 59
2

You can use

HtmlGenericControl table = new HtmlGenericControl("table");

instead of

HtmlTable table = new HtmlTable();
0

graphicdivine has the correct response, that functionality is not available to build the table programatically using htmlcontrols. You could could build it in the markup with html tags and add runat server where needed or you build it programatically using the webcontrols. like Table, TableRow etc...

vernmic
  • 25
  • 5