Cannot Clear HTML Table in ASP.NET
<p>In my <strong>Page Load</strong> Method, I'm filling an HTML Table with Data.</p>

<p>By Default, the Table is empty.</p>

<pre><code>&lt;table runat="server" id="categoriesTable"&gt;
&lt;/table&gt;
</code></pre>

<p>After I create each Row dynamically, I add 4 cells to it, and I give each cell an ID to be used later.</p>

<p>After creating all Cells, I re-traverse this Table so I can add inside each cell a list <code>&lt;li&gt;&lt;/li&gt;</code>, The Data inside these lists are fetched from a Database, which depends on the ID given to each Cell previously.</p>

<p>When I refresh the page, nothing goes wrong, but on <code>PostBack</code>'s (After clicking on some ASP Button, or changing the Selected Index of a DropDownList), the number of cells stays the same, but the list inside each cell is doubled.</p>

<p>Means, if I had this:</p>

<pre><code>Cell1
-Da
-Do
-Di
-Du
Cell2
-Ya
-Yo
Cell3
-Ka
-Ki
</code></pre>

<p>I would have this after the PostBack:</p>

<pre><code>Cell1
-Da
-Do
-Di
-Du
-Da
-Do
-Di
-Du
Cell2
-Ya
-Yo
-Ya
-Yo
Cell3
-Ka
-Ki
-Ka
-Ki
</code></pre>

<p>Here is the code, note that <strong>cCategory</strong> is a class that I created, and its methods return a <code>List&lt;cCategory&gt;</code>.</p>

<pre><code>//Load Categories Into the Table
List&lt;cCategory&gt; mainCategoriesList = cCategory.SubCategories(null);
if(mainCategoriesList.Count!=0)
{
    //Categories Available
    HtmlTableRow Row = new HtmlTableRow();
    categoriesTable.Rows.Add(Row);
    HtmlTableCell Cell;
    int cellCounter = 0;
    while(cellCounter&lt;mainCategoriesList.Count)
    {
        if (cellCounter % 4 == 0 &amp;&amp; cellCounter!=0)
        {
            //Add a New Row
            Row = new HtmlTableRow();
            categoriesTable.Rows.Add(Row);
        }
        Cell = new HtmlTableCell();
        Cell.InnerHtml = "&lt;a href=\"Category.aspx?id=" + mainCategoriesList.ElementAt(cellCounter).CategoryID() + "\"&gt;" + mainCategoriesList.ElementAt(cellCounter).Category()+ "&lt;/a&gt;";
        Cell.ID = mainCategoriesList.ElementAt(cellCounter).CategoryID();
        Row.Cells.Add(Cell);
        cellCounter++;
    }
    //Now we must add the sub categories
    String subList = "";
    String newContents;
    int counter;
    List&lt;cCategory&gt; subCategoriesList;
    for (int i = 0; i &lt; categoriesTable.Rows.Count; i++)
    {
        //For each Row, go through each Cell
        for (int j = 0; j &lt; categoriesTable.Rows[i].Cells.Count; j++)
        {
            //For Each CELL, get the subCategories
            subCategoriesList = cCategory.SubCategories(categoriesTable.Rows[i].Cells[j].ID);
            counter = 0;
            while (counter &lt; subCategoriesList.Count)
            {
                subList = subList + "&lt;li&gt;&lt;a href=\"Category.aspx?id=" + subCategoriesList.ElementAt(counter).CategoryID() + "\"&gt;" + subCategoriesList.ElementAt(counter).Category() + "&lt;/a&gt;&lt;/li&gt;";
                counter++;
            }
            newContents = "&lt;div class=\"subCategoriesList\"&gt;&lt;ul&gt;" + subList + "&lt;/ul&gt;&lt;/div&gt;";
            subList = "";
            categoriesTable.Rows[i].Cells[j].InnerHtml = categoriesTable.Rows[i].Cells[j].InnerHtml + newContents;

        }
    }
}
</code></pre>

<p>Why is the Cell data getting doubled?</p>
