title: Cannot access dynamic Textbox/DropDown controls in a dynamic table inside a Panel
body: <p>Okay so I have this panel inside a table's td.</p>
  
  <p>In this panel I'm creating a dynamic table with text box and drop down controls on a button click.</p>
  
  <p>Problem is that When trying to receive the controls, I FAIL. I've checked literally like more than 25 websites and couldn't find a decent solution or something similar to mine.</p>
  
  <p>Here's the code in which I'm trying to access the controls. It's inside a button event handler. </p>
  
  <pre><code>         for (int i = 0; i &lt; ClickCount; i++)
          {
              //Control myControls = Page.FindControl("table1");
              //Panel myPanel = (Panel)mainTable.FindControl("Panel1");
              //Table myTable = (Table)myPanel.FindControl("table1");
              string FirstName = null;
              string LastName = null;
              string Age = null;
              string PCountry = null;
              string PCommittee = null;
  
              TextBox txtF = (TextBox)Panel1.FindControl("TextBoxF" + i.ToString());
              FirstName = txtF.Text;
  
              TextBox txtL = (TextBox)Panel1.FindControl("TextBoxL" + i.ToString());
              LastName = txtL.Text;
  
              TextBox txtA = (TextBox)Panel1.FindControl("TextBoxA" + i.ToString());
              Age = txtA.Text;
  
              DropDownList ddlPCountry = (DropDownList)myTable.FindControl("ddlPCountry" + i.ToString());
              PCountry = ddlPCountry.SelectedValue;
  
              DropDownList ddlPCommittee = (DropDownList)myTable.FindControl("ddlCommittee" + i.ToString());
              PCommittee = ddlPCommittee.SelectedValue;
  
  
              string groupInsert = "insert into GroupUser (GroupUserID, FirstName, LastName, Age, PreferredCountry, PreferredCommittee) ";
              groupInsert += "values (@GroupUserID, @FirstName, @LastName, @Age, @PreferredCountry, @PreferredCommittee);";
  
              SqlCommand group = conn.CreateCommand();
              group.CommandType = CommandType.Text;
              group.CommandText = groupInsert;
  
              group.Parameters.Add(new SqlParameter("@GroupUserID", ID));
              group.Parameters.Add(new SqlParameter("@FirstName", FirstName));
              group.Parameters.Add(new SqlParameter("@LastName", LastName));
              group.Parameters.Add(new SqlParameter("@Age", Age));
              //group.Parameters.Add(new SqlParameter("@PreferredCountry", ddlPCountry.SelectedValue));
              //group.Parameters.Add(new SqlParameter("@PreferredCommittee", ddlPCommittee.SelectedValue));
  
              int temp = group.ExecuteNonQuery();
  
          }
  
          Response.Redirect("StartPage.aspx");
  </code></pre>
  
  <p>Here's the creation of the controls in another button event handler.</p>
  
  <pre><code>        Table table = new Table();
          table.ID = "table1";
  
          for (int i = 0; i &lt; ClickCount; i++)
          {
              TableRow row1 = new TableRow();
              TableRow row2 = new TableRow();
              TableRow row3 = new TableRow();
              TableRow row4 = new TableRow();
              TableRow row5 = new TableRow();
              TableRow row6 = new TableRow();
              TableRow row7 = new TableRow();
  
              TextBox TxtBoxF = new TextBox();
              TextBox TxtBoxL = new TextBox();
              TextBox TxtBoxA = new TextBox();
  
              DropDownList ddlPCountry = new DropDownList();
              DropDownList ddlPCommittee = new DropDownList();
  
              Label lblF = new Label();
              Label lblL = new Label();
              Label lblA = new Label();
              Label lblPCountry = new Label();
              Label lblPCommittee = new Label();
  
              TxtBoxF.ID = "TextBoxF" + i.ToString();
              TxtBoxL.ID = "TextBoxL" + i.ToString();
              TxtBoxA.ID = "TextBoxA" + i.ToString();
  
              ddlPCountry.ID = "ddlPCountry" + i.ToString();
              ddlPCommittee.ID = "ddlPCommittee" + i.ToString();
  
              lblF.ID = "LabelF" + i.ToString();
              lblL.ID = "LabelU" + i.ToString();
              lblA.ID = "LabelA" + i.ToString();
              lblPCountry.ID = "LabelPCountry" + i.ToString();
              lblPCommittee.ID = "LabelPCommittee" + i.ToString();
  
              lblF.Text = "First Name: ";
              lblL.Text = "Last Name :";
              lblA.Text = "Age: ";
              lblPCountry.Text = "Preferred Country: ";
              lblPCommittee.Text = "Preferred Committee: ";
  
              ddlPCountry.DataSource = countryDt;
              ddlPCountry.DataValueField = "CountryID";
              ddlPCountry.DataTextField = "CountryName";
              ddlPCountry.DataBind();
  
              ddlPCommittee.DataSource = committeeDt;
              ddlPCommittee.DataValueField = "CommitteeID";
              ddlPCommittee.DataTextField = "CommitteeName";
              ddlPCommittee.DataBind();
  
              TxtBoxF.Attributes.Add("runat", "server");
              TxtBoxL.Attributes.Add("runat", "server");
              TxtBoxA.Attributes.Add("runat", "server");
  
              ddlPCountry.Attributes.Add("runat", "server");
              ddlPCommittee.Attributes.Add("runat", "server");
  
              for (int a = 0; a &lt; 2; a++)
              {
                  TableCell cell1 = new TableCell();
                  TableCell cell2 = new TableCell();
  
                  cell1.Controls.Add(lblF);
                  cell2.Controls.Add(TxtBoxF);
                  row1.Controls.Add(cell1);
                  row1.Controls.Add(cell2);
              }
  
              for (int b = 0; b &lt; 2; b++)
              {
                  TableCell cell1 = new TableCell();
                  TableCell cell2 = new TableCell();
  
                  cell1.Controls.Add(lblL);
                  cell2.Controls.Add(TxtBoxL);
                  row2.Controls.Add(cell1);
                  row2.Controls.Add(cell2);
              }
  
              for (int c = 0; c &lt; 2; c++)
              {
                  TableCell cell1 = new TableCell();
                  TableCell cell2 = new TableCell();
  
                  cell1.Controls.Add(lblA);
                  cell2.Controls.Add(TxtBoxA);
                  row3.Controls.Add(cell1);
                  row3.Controls.Add(cell2);
              }
  
              for (int d = 0; d &lt; 2; d++)
              {
                  TableCell cell1 = new TableCell();
                  TableCell cell2 = new TableCell();
  
                  cell1.Controls.Add(lblPCountry);
                  cell2.Controls.Add(ddlPCountry);
                  row4.Controls.Add(cell1);
                  row4.Controls.Add(cell2);
              }
  
              for (int f = 0; f &lt; 2; f++)
              {
                  TableCell cell1 = new TableCell();
                  TableCell cell2 = new TableCell();
  
                  cell1.Controls.Add(lblPCommittee);
                  cell2.Controls.Add(ddlPCommittee);
                  row5.Controls.Add(cell1);
                  row5.Controls.Add(cell2);
              }
  
              table.Rows.Add(row1);
              table.Rows.Add(row2);
              table.Rows.Add(row3);
              table.Rows.Add(row4);
              table.Rows.Add(row5);
  
          }
          Panel1.Controls.Add(table);
          Panel1.Controls.Add(new LiteralControl("&lt;br /&gt;"));
          Panel1.Controls.Add(new LiteralControl("&lt;br /&gt;"));
          conn.Close();
  </code></pre>
  
  <p>Here's the aspx:</p>
  
  <pre><code>&lt;body&gt;
  &lt;form id="form1" runat="server"&gt;
  &lt;div&gt;
  &lt;h1&gt;Group Registration&lt;/h1&gt;
      &lt;br /&gt;
      &lt;table style="width:100%;" id="mainTable" runat="server"&gt;
          &lt;tr&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;First Name:&lt;/td&gt;
              &lt;td&gt;
                  &lt;asp:TextBox ID="txtFirstName" runat="server"&gt;&lt;/asp:TextBox&gt;
              &lt;/td&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Last Name:&lt;/td&gt;
              &lt;td&gt;
                  &lt;asp:TextBox ID="txtLastName" runat="server"&gt;&lt;/asp:TextBox&gt;
              &lt;/td&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Age:&lt;/td&gt;
              &lt;td&gt;
                  &lt;asp:TextBox ID="txtAge" runat="server"&gt;&lt;/asp:TextBox&gt;
              &lt;/td&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td class="auto-style1"&gt;Contact Number:&lt;/td&gt;
              &lt;td class="auto-style1"&gt;
                  &lt;asp:TextBox ID="txtContactNumber" runat="server"&gt;&lt;/asp:TextBox&gt;
              &lt;/td&gt;
              &lt;td class="auto-style1"&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Email Address:&lt;/td&gt;
              &lt;td&gt;
                  &lt;asp:TextBox ID="txtEmail" runat="server"&gt;&lt;/asp:TextBox&gt;
              &lt;/td&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Institute&lt;/td&gt;
              &lt;td&gt;
                  &lt;asp:TextBox ID="txtInstitute" runat="server" Width="400px"&gt;&lt;/asp:TextBox&gt;
              &lt;/td&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td class="auto-style1"&gt;Preferred Country:&lt;/td&gt;
              &lt;td class="auto-style1"&gt;
                  &lt;asp:DropDownList ID="ddlCountry" runat="server"&gt;
                  &lt;/asp:DropDownList&gt;
              &lt;/td&gt;
              &lt;td class="auto-style1"&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Preferred Committee:&lt;/td&gt;
              &lt;td&gt;
                  &lt;asp:DropDownList ID="ddlCommittee" runat="server"&gt;
                  &lt;/asp:DropDownList&gt;
              &lt;/td&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Username:&lt;/td&gt;
              &lt;td&gt;&lt;asp:TextBox ID="txtUsername" runat="server"&gt;&lt;/asp:TextBox&gt;
              &lt;/td&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Password:&lt;/td&gt;
              &lt;td&gt;
                  &lt;asp:TextBox ID="txtPassword" runat="server" TextMode="Password"&gt;&lt;/asp:TextBox&gt;
              &lt;/td&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
              &lt;td&gt;
                  &lt;asp:Panel ID="Panel1" runat="server"&gt;
                  &lt;/asp:Panel&gt;
              &lt;/td&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
              &lt;td&gt;
                  &lt;asp:Button ID="btnAddAnother" runat="server" OnClick="btnAddAnother_Click" Text="Add Another Member" /&gt;
              &lt;/td&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
              &lt;td&gt;
                  &lt;asp:Button ID="btnRegister" runat="server" Text="Sign Up" OnClick="btnRegister_Click" /&gt;
              &lt;/td&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>
  </p>