title: Dynamically created "CheckBoxList" in placeholder controls throwing null reference exception error during page postback
body: <p>I have a web form that will dynamically create a number of checkboxlist filled with data based on the number of row in database. Then this is added to a placeholder to be displayed. </p>
  
  <p>theres a button when clicked, will add the selected check boxes value into database but right now when the button is clicked and after the postback, the page will show the error "System.NullReferenceException".</p>
  
  <p>the following code is written in Page_Load in (!Page.IsNotPostBack) and in a loop that will dynamically create a number of checkboxlist:</p>
  
  <pre><code>                CheckBoxLis chkContent = new CheckBoxList();
                  chkContent.ID = chkIDString; //chkIDString is an incremental int based on the row count 
                  chkContent.RepeatDirection = RepeatDirection.Horizontal;                    
                  foreach (List&lt;t&gt; contentList in List&lt;t&gt;) //data retrieved as List&lt;t&gt; using LINQ
                  {
                      ListItem contents = new ListItem();
                      contents.Text = contentList.Title;
                      contents.Value = contentList.contentID.ToString();
                      chkContent.Items.Add(contents);
                  }
  
                  plcSchool.Controls.Add(chkContent); //plcSchool is my placeholder
                  plcSchool.Controls.Add(new LiteralControl("&lt;br&gt;"));
  
  
  
  protected void btnAdd_Click(object sender, EventArgs e)
  {
      CheckBoxList cbl = Page.FindControl("chkContent4") as CheckBoxList;
      Response.Write(cbl.SelectedValue.ToString()); // now im just testing to get the value from one of the checkboxlist     
  }
  </code></pre>
  
  <p>Anyone able to help, as it seems the controls are not recreated after the postback, therefore it can't find the control and throwing the null reference exception. </p>