文本框表未按要求显示

时间:2009-08-20 16:00:55

标签: asp.net controls

我将texbox添加到表中(类型为Table),但我无法添加它们。我不能在每行添加多个单元格,任何想法?

TextBox[] tx = new TextBox[10];
        TableCell[] tc = new TableCell[10];

        TableRow[] tr = new TableRow[10];

        for (int i = 0; i < 10; i++)
        {
            tx[i] = new TextBox();
            tc[i] = new TableCell();
            tc[i].Controls.Add(tx[i]);
        }

        for (int i = 0; i < 10; i++)
        {
            tr[i] = new TableRow();
            tr[i].Cells.Add(tc[i]);
        }

        for (int i = 0; i < 10; i++)
            Table1.Rows.Add(tr[i]);

它出现了10行,每行只有1个单元

3 个答案:

答案 0 :(得分:1)

因为你需要一个内循环:

for (int i = 0; i < 10; i++)
{
    tr[i] = new TableRow();
    tr[i].Cells.Add(tc[i]);
}

试试这个:

for (int i = 0; i < 10; i++)
{
    tr[i] = new TableRow();
    for (int x = 0; x < 10; x++)
    {
       tr[i].Cells.Add(tc[x]);
    }
}

答案 1 :(得分:0)

您的循环未设置为提供10x10表

Table table = new Table();
TableRow tr = null;
TableCell tc = null;
for (int i = 0; i < 10; i++)
{
    tr = new TableRow();

    for (int j = 0; j < 10; j++)
    {
        tc = new TableCell();

        tc.Controls.Add(new TextBox());

        tr.Cells.Add(tc);
    }

    table.Rows.Add(tr);
}

答案 2 :(得分:0)

细胞必须是不同的:我需要创建100个细胞,不仅仅是10个!

TextBox[] tx = new TextBox[100];
        TableCell[] tc = new TableCell[100];

        TableRow[] tr = new TableRow[10];

        for (int i = 0; i < 100; i++)
        {
            tx[i] = new TextBox();
            tc[i] = new TableCell();
            tc[i].Controls.Add(tx[i]);
        }

        int x = 0;
        for (int i = 0; i < 10; i++)
        {
            tr[i] = new TableRow();
            for (int j=0; j < 10; j++)
            {
                tr[i].Cells.Add(tc[x++]);
            }
        }


        for (int i = 0; i < 10; i++)
            Table1.Rows.Add(tr[i]);