1

I'm trying to create a table dynamically through the backend. I've stepped through my code and cannot find any problems. The table simply doesn't show. Here is the code -

Table tblEmployeeList = new Table();

foreach (DataRow row in dtEmployList.Rows)
{
    TableRow tRow = new TableRow();

    foreach(DataColumn dCol in dtEmployList.Columns)
    {
        TableCell tCell = new TableCell();
        tCell.Text = row["Username"].ToString();
        tRow.Cells.Add(tCell);
    }
    tblEmployeeList.Rows.Add(tRow);
}
Sarath S Menon
  • 2,168
  • 1
  • 16
  • 21

1 Answers1

1

You have created a html table on server side but did not added in the html of the page. Add a div in HTML make it runat="server". Add the current table in DIV after you are finished adding rows.

In HTML

<div id="div1" runat="server"></div>

In code bahind

div1.Controls.Add(tblEmployeeList); //This will show the table in the page

Your code would be

Table tblEmployeeList = new Table();
foreach (DataRow row in dtEmployList.Rows)
{
        TableRow tRow = new TableRow();

        foreach(DataColumn dCol in dtEmployList.Columns)
        {
            TableCell tCell = new TableCell();
            tCell.Text = row["Username"].ToString();
            tRow.Cells.Add(tCell);
        }

        tblEmployeeList.Rows.Add(tRow);
 }
div1.Controls.Add(tblEmployeeList); //This will show the table in the page
Adil
  • 146,340
  • 25
  • 209
  • 204