动态表未显示

时间:2016-06-23 09:42:31

标签: c# asp.net

我试图通过后端动态创建表。我已经完成了我的代码并且找不到任何问题。表格根本没有显示。这是代码 -

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

1 个答案:

答案 0 :(得分:1)

您已在服务器端创建了一个html表,但未在页面的html中添加。在HTML中添加div使其成为runat =“server”。完成添加行后,在DIV中添加当前表。

在HTML中

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

代码bahind

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

您的代码将是

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