Search code examples
asp.netwebformsrowservercontrol

Add generated rows and columns to table in aspx file


I want to add my table to the page, and the Table control is put in the aspx file and the columns and rows is generated in the aspx.cs file:

if (Page.IsValid)
        {
            var start = Int32.Parse(TextBox1.Text);
            var slut = Int32.Parse(TextBox2.Text);
            var diff = Int32.Parse(TextBox3.Text);

            for(int i = 0; i < (slut - start) / diff; i++)
            {
                TableRow tr = new TableRow();
                tr.ID = "row" + i.ToString();
                for (int j = 0; j <= 2; j++)
                {
                    TableCell tc = new TableCell();
                    tc.ID = "cell" + j.ToString();

                    tr.Cells.Add(tc);
                }
                ??.Rows.Add(tr);
            }
        }

And in aspx:

<asp:Table ID="Table" runat="server" Visible="true">

</asp:Table>

What should I write where the "??" is? ID doesn't work.


Solution

  • First change the ID of your table to:

    <asp:Table ID="MyTable" runat="server" Visible="true">
    

    Then:

    MyTable.Rows.Add(tr);
    

    I think ASP.NET is getting confused because it has a control with the name Table and you tried to identify your table with ID="Table".