Skip to content Skip to sidebar Skip to footer

Populating HtmlTable Control With HtmlTable Object In ASP/C#

It apparently doesn't seem to be an impossible question, but I can't find a way to populate an HtmlTable control (which is accessible from codebehind) with an HtmlTable object alre

Solution 1:

It sounds like you have a table and you're trying to do something like

HtmlTable myTable = new HtmlTable();
this.myPageTable = myTable;

That's not how it works. If you have a populated table object you'd like to render then create a literal control and access it's Controls collection Add() method and add the table object. Like this:

HtmlTable myTable = new HtmlTable();
this.myLiteral.Controls.Add(myTable);

Or you should just populate the table directly.


Solution 2:

Are you using <table> tags to create the table? If so, you should use the <asp:Table> tag and that will give you access to the table from the codebehind.


Solution 3:

A better solution would be passing the HtmlTable from the page to whatever function it is that is populating it, if possible.


Solution 4:

You're just creating work for yourself. There's no need to create all the rows in a local variable when you have a reference to the final destination right there.

Try this:

foreach (Evaluation eval in theEvaluations)
{
    HtmlTableRow anEvaluation = new HtmlTableRow();

    cell1.InnerText = eval.attr1;
    anEvaluation.Cells.Add(cell1);

    cell1.InnerText = eval.attr1;
    anEvaluation.Cells.Add(cell2);

    table1.Rows.Add(anEvaluation);
}

Post a Comment for "Populating HtmlTable Control With HtmlTable Object In ASP/C#"