ASP.NET:以编程方式插入表格单元格

时间:2014-02-19 14:05:08

标签: html asp.net css web

我有一个返回List<string>的方法,我的代码用它来填充表格:

string[] siteNames;
siteNames = client.GetListSiteElementNames();

foreach (string siteName in siteNames)
{
    TableCell tempCell = new TableCell();
    tempCell.Text = siteName;
    this.sitesTableRow.Cells.Add(tempCell);
}

这会将单元格插入到一行中,因此单元格会在屏幕上水平显示(我希望如何),但因为它们大约是20个左右,所以它们会离开页面意味着您需要滚动查看其余部分。

我有什么方法可以做到这一点,以便细胞在窗口边缘切断并进入新线?

2 个答案:

答案 0 :(得分:0)

string[] siteNames;
siteNames = client.GetListSiteElementNames();

foreach (string siteName in siteNames)
{
    TableCell tempCell = new TableCell();
    /* Here add a new line */
    tempCell.Text = siteName + Environment.NewLine;
    this.sitesTableRow.Cells.Add(tempCell);

}

编辑:我的错误。我误解了你的要求。

答案 1 :(得分:0)

要执行此操作,您需要在某个截止点(即一定数量的列)之后添加新行

在下面的示例中,我使用了5的截止:

string[] siteNames;
siteNames = client.GetListSiteElementNames();

// Number of cells on each row
int cutOffPoint = 5;
int currentNum = 1;

foreach (string siteName in siteNames) {
    TableCell tempCell = new TableCell();
    tempCell.Text = siteName;

    if (currentNum > cutOffPoint) {
        currentNum = 1;

        // Code to add new row to table
    }    

    this.sitesTableRow.Cells.Add(tempCell);

    currentNum++;
}

因为我不确定this对象是指什么,或sitesTableRow我不能包含添加行的代码,但想法是在6个单元格后你会重置currentNum变量并添加新行,并将单元格附加到此新行。