title: asp:table losing Data on postback
body: <p><a href="https://i.stack.imgur.com/RxqHj.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RxqHj.jpg" alt="enter image description here"></a></p>
  
  <p>I am using asp:table in my project.I am adding the rows dynamically in the table.</p>
  
  <p>The very first header row is inserted in the page load event.</p>
  
  <p>The Header row has some checkboxes. When any checkbox is checked, a new row is inserted in the <code>checkedChange()</code> event.</p>
  
  <p>This new row has a dynamically created textbox in which I am taking some input from the user.</p>
  
  <p>After inputs given, the user has to click the <code>Add Data</code> button which inserts the data in database.</p>
  
  <p>The row which was created in the <code>OnCheckedChanged</code> event gets deleted when any of the <code>postback</code> occurs.</p>
  
  <p>I tried EnableViewState but it's still not working.</p>
  
  <p>How can I resolve this?</p>
  
  <p>Here is my aspx code:</p>
  
  <pre><code>&lt;asp:Table ID="tbl_fundtype" runat="server" CssClass="table-bordered tblfund" style="width:50%; white-space:nowrap;" EnableViewState="true"&gt;
                          &lt;/asp:Table&gt;
  </code></pre>
  
  <p>Here is the .cs Code for CheckedChangedEvent:</p>
  
  <pre><code>public void Chk_Fund_CheckedChange(object sender, EventArgs e)
      {
          TableRow tr = new TableRow();
          for (int i = 0; i &lt; tbl_fundtype.Rows[0].Cells.Count; i++)
          {
              string DynamicChkID = "fund_" + dt_fundtype.Rows[i]["fund_type_cd"].ToString();
              CheckBox chk = new CheckBox();
              chk = (CheckBox)tbl_fundtype.Rows[0].Cells[i].FindControl(DynamicChkID);
              TableCell td = new TableCell();
              if (chk.Checked == true)
              {
                  TextBox txt = new TextBox();
                  txt.ID = "txt_" + dt_fundtype.Rows[i]["fund_type_cd"].ToString();
                  txt.Attributes.Add("Placeholder","Enter Share Percent...");
                  td.Controls.Add(txt);
              }
              else
              {
                  td.Text = "     ";
              }
              tr.Cells.Add(td);
          }
          tbl_fundtype.Rows.Add(tr);
          hfTab.Value = "fund";
          collapsestate = "expand";
      }
  </code></pre>
  
  <p>Here I am trying to get the values from the Created Rows in checked changed event on button click.</p>
  
  <pre><code>public bool ValidateAddNominee()
      {
          if (txt_nomineename.Text == "")
          {
              lbl_nomineeErr.Text = "Please Enter Nominee Name";
              return false;
          }
          else if (txt_nomineecnic.Text == "")
          {
              lbl_nomineeErr.Text = "Please Enter Nominee CNIC";
              return false;
          }
          //Count if any of the nominee is selected
          int countShares = 0;
          for (int i = 0; i &lt; tbl_fundtype.Rows[0].Cells.Count; i++)
          {
              string DynamicChkID = "fund_" + dt_fundtype.Rows[i]["fund_type_cd"].ToString();
              CheckBox chk = new CheckBox();
              chk = (CheckBox)tbl_fundtype.Rows[0].Cells[i].FindControl(DynamicChkID);
              if (chk.Checked == true)
              {
                  countShares++;
                  TextBox txt = new TextBox();
                  string DynamicTxtID = "txt_" + dt_fundtype.Rows[i]["fund_type_cd"].ToString();
                  int chek = tbl_fundtype.Rows.Count;
                  txt = (TextBox)tbl_fundtype.Rows[1].Cells[i].FindControl(DynamicTxtID);
                  if (txt.Text=="")
                  {
                      lbl_nomineeErr.Text = "Please Enter Share Percent for "+ dt_fundtype.Rows[i]["fund_type"].ToString();
                      return false;
                  }
              }           
          }
          if (countShares == 0)
          {
              lbl_nomineeErr.Text = "Please Select any Fund Type";
              return false;
          }
          else if (!file_nominee.HasFile)
          {
              lbl_nomineeErr.Text = "Please attach Nominee CNIC";
              return false;
          }
          else
          {
              return true;
          }
  
  
  
  
      }
  </code></pre>
  
  <p>Here is the Header row is inserted in Page load event :</p>
  
  <pre><code>TableRow thead = new TableRow();
          for (int i = 0; i &lt; dt_fundtype.Rows.Count; i++)
          {
              TableCell td = new TableCell();
              CheckBox chk = new CheckBox();
              chk.ID = "fund_" + dt_fundtype.Rows[i]["fund_type_cd"];
              chk.Text = dt_fundtype.Rows[i]["fund_type"].ToString();
              chk.AutoPostBack = true;
              chk.CheckedChanged += new EventHandler(Chk_Fund_CheckedChange);
              td.Controls.Add(chk);
              thead.Cells.Add(td);
          }
          tbl_fundtype.Rows.Add(thead);
  </code></pre>
  
  <p>It's Showing me the Row count is 1 but it should be two, Number one the Header Row and the other should be the row which is created in checked changed event.</p>