tags:

views:

500

answers:

6

Hello,

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 already filled with data.

Can anybody help me on this ??

Regards,

EDIT

In the ASP page, I have an HTML table with ID="table1"

In codebehind, I first populate an HtmlTable Object from a List doing like this :

HtmlTable localTable1 = new HtmlTable();
HtmlTableCell cell1 = new HtmlTableCell();
HtmlTableCell cell2 = new HtmlTableCell();

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

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

    cell2.InnerText = eval.attr2;
    anEvaluation.Cells.Add(cell2);

    localTable1.Rows.Add(anEvaluation);
}

// And eventually here I should pass the localTable1 to table1
A: 

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.

JohnathanKong
+1  A: 

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.

Spencer Ruport
Thanks Spencer, but the thing is I already have a precise table frame settled in my ASP page (Headers <th> are specified in it) and I just wanted to add rows below
Then you need to access the rows collection of the table object and create new HtmlTableRow() objects instead of a whole new table.
Spencer Ruport
Ok, that's what I do now, and it adds a row to my ASP table but always and only the last record of my source list (which contains 2 elements) ...
A: 

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

Pike65
A: 

In the ASP page, I have an HTML table with ID="table1"

In codebehind, I first populate an HtmlTable Object from a List doing like this :

HtmlTable localTable1 = new HtmlTable();
HtmlTableCell cell1 = new HtmlTableCell();
HtmlTableCell cell2 = new HtmlTableCell();

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);

    localTable1.Rows.Add(anEvaluation);
}

// And eventually here I should pass the localTable1 to table1
You should edit your question if you need to add information instead of submitting an answer. I appended this to your question so you should just go ahead and delete this answer.
Spencer Ruport
A: 

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);
}
Pike65
Yes! I already tried this but only the last added cell is included ...
So the only way I found to add every different columns is to instantiate an HtmlTableCell for each one.But then I have the same problem with the columns : Only the last one of my List "theEvaluations" is present in the ASP table at the end.I checked the List.Count and there is more than one record in it ...
A: 

Ok, I eventually have everything working, le remaining problem was just due to a wrong declaration place for the HtmlTableCell objects.

Here is a working snippet :

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


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

    cell2.InnerText = eval.attr2;
    anEvaluation.Cells.Add(cell2);

    table1.Rows.Add(anEvaluation);  // table1 : the real HTML table in ASP page
}