Table generation from Code Behind
<p>I know what my fault is, but not sure how to resolve. I am trying to generate an asp:table from Code Behind.</p>

<p>The table should be 3 cells wide... I'll work on the row limit later.</p>

<p>Here's my code:</p>

<pre><code>GallaryImage g = new GallaryImage();
        var images = g.GetAll();

        photos.Style.Add("width","100%");
        photos.Style.Add("border-style","none");

        TableRow tr = new TableRow();
        TableCell tc = new TableCell();
        tr.Cells.Add(tc);
        tr.Cells.Add(tc);
        tr.Cells.Add(tc);

        int cntr = 0;
        TableRow row = new TableRow();

        foreach (var image in images)
        {
            cntr++;
            TableCell cell = new TableCell();
            Image i = new Image();
            i.ImageUrl = image.fullThumbPath;
            cell.Controls.Add(i);
            row.Cells.Add(cell);
            if(cntr%3==0)
            {
                photos.Rows.Add(row);
                row.Cells.Clear();
            }
        }
        if(row.Cells.Count &gt; 0)
            photos.Rows.Add(row);
    }
</code></pre>

<p>My problem is that I need to create a new row in the Foreach, only when I need the new row... i.e, when we have added 3 cells.</p>

<p>I thought I could add the row to the table, and then clear the row to start a new row - but that's not working, as I just keep clearing the same row object... and therefore, never add multiple rows.</p>

<p>Can someone assist with my logic here?</p>
