title: saving values of textbox in a loop (asp.net web page)
body: <p>I've been trying to follow a few tutorials and code examples but something isn't working as I'd expect. </p>
  
  <p>I have a series of textboxes on a webpage and I need to loop through each one and save it's value to the database. The number of textboxes on the page will vary. I load them from a database. They are all added to a table object.</p>
  
  <pre><code>TableCell cellb = new TableCell();
  TextBox txtAnswer = new TextBox();
  txtAnswer.TextMode = TextBoxMode.MultiLine;
  txtAnswer.Rows = 2;
  txtAnswer.ID = "field_" + dataRow["fieldID"].ToString();
  txtAnswer.Text = "answer"; //this will be got from the database
  
  cellb.Controls.Add(txtAnswer);
  </code></pre>
  
  <p>so that adds the textbox to the table row. I then have a save button which has the following code</p>
  
  <pre><code>foreach (Control c in Page.Controls)
  {
      foreach (Control childc in c.Controls) 
      {
          if (childc is TextBox)
          {
              TextBox tmpText = (TextBox)childc;
              tmpField = tmpText.ID.Split('_');
              fieldID = Convert.ToInt32(tmpField[1]);
              //save value to the database (eventually)
              debug.InnerHtml += tmpText.Text; //this just outputs the values for now
          }
      }
  }
  </code></pre>
  
  <p>So the above should loop though all the page controls and find the textfields as added on the page_load. However, I am now wondering if it's because they don't exist. So when I save the page, it doesn't know of the controls. I can see the table control, but nothing inside it.... any ideas?!</p>