title: How to get values from dynamically generated controls in asp.net c#?
body: <p>I know this is a well asked question and I found some marked as answers but those doesn't solve my problem. Please have a look at my codes..</p>
  
  <p><strong>Method to Display dynamic controls</strong></p>
  
  <pre><code>private void ShowControlsByFormId()
              {
                  List&lt;FormControlsBO&gt; list = new List&lt;FormControlsBO&gt;();
  
                  list = new FormControlsDA().FormControls_GetByFormId(Convert.ToInt32(ddlForm.SelectedValue.ToString()));
  
                  if (list.Count &gt; 0)
                  {
                      for (int i = 0; i &lt; list.Count; i++)
                      {
                          DynamicControl dynamicControl = CommonUtility.GenerateControl(list[i]);
                          pnlInput.Controls.Add(new LiteralControl("&lt;tr&gt;&lt;td&gt;"));
                          pnlInput.Controls.Add(dynamicControl.GeneratedControlLiteral);
                          pnlInput.Controls.Add(new LiteralControl("&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;"));
                          pnlInput.Controls.Add(dynamicControl.GeneratedControl);
                          pnlInput.Controls.Add(new LiteralControl("&lt;/td&gt;&lt;tr&gt;&lt;br/&gt;&lt;br/&gt;"));
                      }
                      pnlAction.Visible = true;
                  }
                  else
                  {
                      pnlAction.Visible = false;
                  }
              }
  </code></pre>
  
  <p><strong>Method to Generate dynamic controls</strong></p>
  
  <pre><code>public static DynamicControl GenerateControl(FormControlsBO bo)
          {
              DynamicControl dynamicControl = new DynamicControl();
              Control control = new Control();
  
              LiteralControl literal = new LiteralControl();
  
              switch (bo.FieldType)
              {
                  case "TextBox":
                      control = new TextBox();
                      control.ID = bo.FieldName;
                      literal.Text = bo.FieldLabel;
                      break;
                  case "RadioButton":
                      control = new RadioButton();
                      control.ID = bo.FieldName;
                      literal.Text = bo.FieldLabel;
                      break;
                  case "CheckBox":
                      control = new CheckBox();
                      control.ID = bo.FieldName;
                      literal.Text = bo.FieldLabel;
                      break;
                  case "DropDownList":
                      control = new DropDownList();
                      control.ID = bo.FieldName;
                      literal.Text = bo.FieldLabel;
                      break;
              }
  
              control.ClientIDMode = ClientIDMode.Static;
              dynamicControl.GeneratedControl = control;
              dynamicControl.GeneratedControlLiteral = literal;
  
              return dynamicControl;
          }
  </code></pre>
  
  <p><strong>Method to Save data</strong></p>
  
  <pre><code>    private void FormRecords_Save()
              {
                  List&lt;FormControlsBO&gt; list = new List&lt;FormControlsBO&gt;();
                  FormControlsBO bo = new FormControlsBO();
  
                  foreach (Control ctl in pnlInput.Controls)
                  {
                      CommonUtility.DataFiller(bo, ctl);
                      list.Add(bo);
                  }
  
                  Boolean result = false;
                  result = new FormControlsDA().FormRecords_Save(list);
  
                  if(result == true)
                  {
                      lblMessage.Text = "Form data saved successfully";
                  }
                  else
                  {
                      lblMessage.Text = "Form data not saved";
                  }
              }
  </code></pre>
  
  <p>The problem is, when I debug the code, <code>pnlInput.Controls</code> shows Zero count. Please help !! </p>