Add rows to a table dynamically

asp.net, html-table

Solution

It is working, I have tested it:

In my page load, I have:

 protected void Page_Load(object sender, EventArgs e)
        {
            TableRow row = new TableRow();
            TableCell cell = new TableCell();
            cell.Controls.Add(new TextBox());
            row.Cells.Add(cell);
            table.Rows.Add(row);
        }

On my aspx page, I have:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:Table ID="table" runat="server" /> 
</asp:Content>

The html rendered by this code:

<table id="MainContent_table">
    <tbody><tr>
        <td><input name="ctl00$MainContent$ctl00" type="text"></td>
    </tr>
</tbody></table>

Problem

At the moment I am trying to create a table whose content is supposed to be created by a subclass (result of querying a RESTful web service). I have been working for quite some time on that now and I just cannot seem to get it to work. I have tried so many different solutions. - creating the table in the subclass and add it to Page.Controls. That gets me "The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)." which does not make sense at all. - I have tried to create an empty table on the page and passed its handle to the subclass which was responsible for adding rows. Noting happened. - I have tried to return a TableRowCollection and assigned it to the previously created (empty) table. Nothing happened. Now I just want to add one row and one cell to the table (baby steps towards what I need). Not even that works. Please find the code for that attached: ``` TableRow row = new TableRow(); table.Rows.Add(row); TableCell cell = new TableCell(); row.Cells.Add(cell); cell.Controls.Add(new TextBox()); ``` The table is simple and empty: ``` <asp:Table ID="table" runat="server" /> ``` The source code that is displayed by my browser looks like that: ``` <table id="table"> </table> ``` I have been looking at countless examples on the web and all look like this. I guess it is only a tiny problem somewhere but I have not been able to figure it out. Now I am willing to offer life-long gratitude to everybody who can provide the hint for solving this mess.

Original source