title: On post-back dynamically added controls are replaced
body: <p>Okay, so I have a table on my ASP page like so: </p>
  
  <pre><code>&lt;asp:Table ID="attachedFiles" runat="server" BorderColor="LightGray" BorderWidth="1" ViewStateMode="Enabled"&gt; 
      &lt;asp:TableRow ID = "attachedFilesRow" runat="server"&gt;
          &lt;asp:TableCell ID="exampleCell" runat="server" Visible="false" &gt;
              &lt;asp:Label runat="server" Text="" /&gt;
              &lt;asp:ImageButton runat="server" ImageAlign="Middle" ImageUrl="~/images/kill.png" OnClick="removeAttachment" /&gt;
          &lt;/asp:TableCell&gt;
      &lt;/asp:TableRow&gt;
  &lt;/asp:Table&gt;
  </code></pre>
  
  <p>The example cell I create here is not visible because I am only using it as a template for other cells that I will be adding in the code behind: </p>
  
  <pre><code>TableCell cell = new TableCell();
  TableRow row = new TableRow(); 
  
  cell = exampleCell;
  row = attachedFilesRow;
  
  ((Label)cell.Controls[0]).Text = fileName;
  cell.Visible = true;
  row.Cells.Add(cell);
  attachedFilesRow = row;
  attachedFilesRow.DataBind();
  </code></pre>
  
  <p>The code in the code behind is in an onClick method for a button on the ASP page. The idea is that if the user wants to add an attachment, they click 'Add attachment' and then a cell is added to the already existing table. The cell is supposed to look like this [filenName.txt X] where the X is an ImageButton so that the user can click it and delete the attachment. </p>
  
  <p>My problem is that everytime the user adds an attachment, the previous TableCell is replaced with the new TableCell instead of added to the TableRow. I thought that maybe it was because I was creating a TableCell and then setting it to equal an already existing table cell instance so I tried this: </p>
  
  <p>ASP:</p>
  
  <pre><code>&lt;asp:Table ID="attachedFiles" runat="server" BorderColor="LightGray" BorderWidth="1" ViewStateMode="Enabled"&gt; 
      &lt;asp:TableRow ID = "attachedFilesRow" runat="server"&gt;
      &lt;/asp:TableRow&gt;
  &lt;/asp:Table&gt;
  </code></pre>
  
  <p>Code Behind:</p>
  
  <pre><code>TableCell cell = new TableCell();
  
  ImageButton button = new ImageButton();
  button.ImageUrl = "~/images/kill.png";
  button.Attributes.Add("onclick", "removeAttachment");
  
  Label label = new Label();
  label.Text = fileName;
  
  cell.Controls.Add(button);
  cell.Controls.Add(label);
  
  attachedFilesRow.Cells.Add(cell); 
  </code></pre>
  
  <p>I have also tried both ways with adding attachedFilesRow.DataBind(); to the end and have tried both methods with instantiating new Row and Cell Objects istead of just directly using existing ones from the ASP page. Neither of those things seems to play much of a role. </p>
  
  <p><strong>tl;dr</strong> Every time I add a TableCell to my existing table on buttonClick, it replaces the other cell in my table instead of adding to it. </p>
  
  <p>EDIT:</p>
  
  <p>I don't necessarily want to add a new Row... This is what I want: </p>
  
  <p>After the user adds one attachment: [fileName.txt X] </p>
  
  <p>After two: [fileName.txt X] [fileName2.txt X] </p>
  
  <p>After three: [fileName.txt X][fileName2.txt X][fileName3.txt X] </p>
  
  <p>It's basically a on Row table, adding columns or cells if that makes sense.</p>