title: ASP.Net C# - Remove controls dynamically
body: <p>I dynamically create a RadioButtonList or a CheckBoxList depending on a condition within a Button_Click event (which works).</p>
  
  <pre><code>protected void btnGetQuestion_Click(object sender, EventArgs e)
  {
      string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
      MySqlConnection conn = new MySqlConnection(connStr);
      MySqlDataReader reader;
  
      List&lt;string&gt; listOfAnswerIDs = new List&lt;string&gt;();
      List&lt;string&gt; listOfAnswers = new List&lt;string&gt;();
      List&lt;string&gt; listOfCorrectAnswerIDs = new List&lt;string&gt;();
  
      int questionCounter = 0;
  
      //get questionIDs and store in ViewState["listOfQuestionIDs"]
      getListOfQuestionIDs();
  
      try
      {
          conn.Open();
  
          string cmdText = "SELECT * FROM questions_m WHERE question_id=@QuestionID";
          MySqlCommand cmd = new MySqlCommand(cmdText, conn);
          cmd.Parameters.Add("@QuestionID", MySqlDbType.Int32);
          cmd.Parameters["@QuestionID"].Value = listOfQuestionIDs[questionCounter];
  
          reader = cmd.ExecuteReader();
  
          if (reader.Read())
          {
              lblQuestion.Text = reader["question"].ToString();
  
              if (reader["type"].ToString().Equals("C"))
              {
                  CheckBoxList cblAnswers = new CheckBoxList();
                  cblAnswers.ID = "cblAnswers";
                  Page.Form.Controls.Add(cblAnswers);
              }
              else if (reader["type"].ToString().Equals("R"))
              {
                  RadioButtonList rblAnswers = new RadioButtonList();
                  rblAnswers.ID = "rblAnswers";
                  Page.Form.Controls.Add(rblAnswers);
              }
  
              questionCounter += 1;
              ViewState["questionCounter"] = questionCounter;
              ViewState["QuestionID"] = Convert.ToInt32(reader["question_id"]);
              reader.Close();
  
              string cmdText2 = "SELECT * FROM answers WHERE question_id=@QuestionID";
              MySqlCommand cmdAnswers = new MySqlCommand(cmdText2, conn);
              cmdAnswers.Parameters.Add("@QuestionID", MySqlDbType.Int32);
              cmdAnswers.Parameters["@QuestionID"].Value = ViewState["QuestionID"];
  
              reader = cmdAnswers.ExecuteReader();
  
              while (reader.Read())
              {
                  listOfAnswerIDs.Add(reader["answer_id"].ToString());
                  listOfAnswers.Add(reader["answer"].ToString());
              }
  
              reader.Close();
              populateAnswers(listOfAnswers, listOfAnswerIDs);
          }
          reader.Close();
      }
      catch
      {
          lblError.Text = "Database connection error - failed to read records.";
      }
      finally
      {
          conn.Close();
      }
  }
  </code></pre>
  
  <p>I want to create a method that I can run in another button click event (btnNext_Click) that will remove the RadioButtonList or CheckBoxList if one exists.</p>
  
  <p>I've tried the following but it doesn't seem to work:</p>
  
  <pre><code>protected void clearAnswers()
  {
      if (((CheckBoxList)this.FindControl("cblAnswers")) != null)
      {
          Page.Form.Controls.Remove(this.FindControl("cblAnswers"));
      }
  
      if (((RadioButtonList)this.FindControl("rblAnswers")) != null)
      {
          Page.Form.Controls.Remove(this.FindControl("rblAnswers"));
      }
  }
  </code></pre>
  
  <p>UPDATE:
  I think the issue I had occured with the repopulation of the RadioButtonList/CheckBoxList. If I cleared each item before repopulating them, that resolved my problem.</p>
  
  <pre><code>    if (((CheckBoxList)this.FindControl("cblAnswers")) != null)
      {
          ((CheckBoxList)this.FindControl("cblAnswers")).Items.Clear();
          foreach (int num in numbers)
          {
              ((CheckBoxList)this.FindControl("cblAnswers")).Items.Add(ans[num - 1]);
          }
      }
  
      if (((RadioButtonList)this.FindControl("rblAnswers")) != null)
      {
          ((RadioButtonList)this.FindControl("rblAnswers")).Items.Clear();
          foreach (int num in numbers)
          {
              ((RadioButtonList)this.FindControl("rblAnswers")).Items.Add(ans[num - 1]);
          }
      }
  </code></pre>