Dynamic table not displayed

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

Upvotes: 1

Views: 556

Answers (1)

Adil
Adil

Reputation: 148150

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

Upvotes: 1

Related Questions