title: Getting value from dynamic ASP.NET text box within HTML table
body: <p>I'm trying to reference the text within an ASP.NET text box that has been loaded on button click within an HTML table. The table is static but then the rows are generated dynamically with the text boxes. I've re-loaded the controls and the values remain in the text boxes on postbox, but I'm getting the following error - 'Object reference not set to an instance of an object.' I'm assuming this is because it hasn't managed to locate the textbox in the table properly. Only thing worth mentioning is that I'm not using System.Web.UI.HtmlControls Namespace (as I seemed to get allot of other errors when I did), so I'm having to call that each time. Does this have something to do with it?</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Data;
  using System.Web.UI.WebControls;
  using System.Net;
  using Newtonsoft.Json;
  
  namespace Generic_name
  {
      public partial class Generatorwebform : System.Web.UI.Page
      {
          protected void Page_init(object sender, EventArgs e)
          {
  
          }
          protected void Page_Load(object sender, EventArgs e)
          {
              if (this.IsPostBack)
              {
                  Response.Write("This is a postback");
              }
  
              if (Convert.ToString(ViewState["Generated"]) == "true")
                  Getmarketdata();
  
          }
  
          protected void Getdatabtn_Click(object sender, EventArgs e)
          {
              Getmarketdata();
  
  
          }
  
          protected void Generatedatabtn_Click(object sender, EventArgs e)
          {
              Response.Write("Generate data button clicked");
              if (Convert.ToString(ViewState["Generated"]) != "true")
              {
                  Getmarketdata();
                  ViewState["Generated"] = "true";
              }
              else
              {
                  Response.Write("viewstate already generated");
              }
              int GeneratorTableRows = GeneratorTable.Rows.Count;
              Response.Write(GeneratorTableRows - 1);
  
  
  ///THIS PART IS THE PROBLEM
              System.Web.UI.HtmlControls.HtmlTable Htmlgeneratortable = ((System.Web.UI.HtmlControls.HtmlTable)GeneratorTable);
              TextBox txt = Htmlgeneratortable.Rows[1].FindControl("tbBbl") as TextBox;
              Response.Write(txt.Text);
          }
  
  
          public void Getmarketdata()
          {
              String url = "https://www.genericapi.com";
              Response.Write(url);
  
              using (var webclient = new WebClient())
              {
                  String Rawjson = webclient.DownloadString(url);
                  dynamic dobj = JsonConvert.DeserializeObject&lt;dynamic&gt;(Rawjson);
                  int noofrecords = dobj["properties"].Count;
                  Response.Write("&lt;br /&gt;" + noofrecords);
                  for (int i = 0; i &lt; noofrecords; i++)
                  {
                      System.Web.UI.HtmlControls.HtmlTableRow tRow = new System.Web.UI.HtmlControls.HtmlTableRow();
                      String RMlink = String.Format("https://www.genericapi.co.uk" + dobj["data"][i]["identifier"].ToString()) + ".html";
                      HyperLink hypLink = new HyperLink();
                      hypLink.Text = dobj["data"][i]["identifier"].ToString();
                      hypLink.Target = "_blank";
                      hypLink.NavigateUrl = RMlink;
                      using (System.Web.UI.HtmlControls.HtmlTableCell tb1 = new System.Web.UI.HtmlControls.HtmlTableCell())
                      {
                          tRow.Cells.Add(tb1);
                          tb1.Controls.Add(hypLink);
                      }
                      using (System.Web.UI.HtmlControls.HtmlTableCell tb2 = new System.Web.UI.HtmlControls.HtmlTableCell())
                      {
                          TextBox tbEPCe = new TextBox();
                          tRow.Cells.Add(tb2);
                          tb2.Controls.Add(tbEPCe);
                          tbEPCe.Style.Add("background", "none"); tbEPCe.Style.Add("border", "1px solid black"); tbEPCe.Style.Add("border-radius", "2px");
                      }
  
                      using (System.Web.UI.HtmlControls.HtmlTableCell tb3 = new System.Web.UI.HtmlControls.HtmlTableCell())
                      {
                          TextBox tbEPCp = new TextBox();
                          tRow.Cells.Add(tb3);
                          tb3.Controls.Add(tbEPCp);
                          tbEPCp.Style.Add("background", "none"); tbEPCp.Style.Add("border", "1px solid black"); tbEPCp.Style.Add("border-radius", "2px");
  
                      }
                      using (System.Web.UI.HtmlControls.HtmlTableCell tb4 = new System.Web.UI.HtmlControls.HtmlTableCell())
                      {
                          TextBox tbBbl = new TextBox();
                          tRow.Cells.Add(tb4);
                          tb4.Controls.Add(tbBbl);
                          tbBbl.Style.Add("background", "none"); tbBbl.Style.Add("border", "1px solid black"); tbBbl.Style.Add("border-radius", "2px");
                      }
  
                      GeneratorTable.Rows.Add(tRow);
                      ViewState["Generated"] = "true";
                  }
  
              }
          }
  
      }
  }
  </code></pre>