title: Dynamically added controls missing during GridView RowUpdating
body: <p>I have a GridView with a TemplateField column that I put PlaceHolder controls in.  During the DataBound event for the GridView I dynamically add a few CheckBoxes to the PlaceHolder.  That works fine and displays as expected.</p>
  
  <p>My problem is that during the RowUpdating event the PlaceHolder contains no controls; my CheckBoxes are missing.  I also noticed that they're missing during the RowEditing event.</p>
  
  <p>I want to be able to get the values of the CheckBoxes during the RowUpdating event so I can save them to the database.</p>
  
  <p>Here's some example code.  I've trimmed out a lot to reduce size, but if you want to see specifics just ask and I'll be happy to add more.</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;asp:GridView ID="gridView" runat="server" AutoGenerateColumns="False" 
      ondatabound="gridView_DataBound" onrowupdating="gridView_RowUpdating" 
      onrowediting="gridView_RowEditing" DataKeyNames="ID"&gt;
      &lt;Columns&gt;
          &lt;asp:TemplateField HeaderText="Countries"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:PlaceHolder ID="countriesPlaceHolder" runat="server"&gt;&lt;/asp:PlaceHolder&gt;
              &lt;/ItemTemplate&gt;
              &lt;EditItemTemplate&gt;
                  &lt;asp:PlaceHolder ID="countriesPlaceHolder" runat="server"&gt;&lt;/asp:PlaceHolder&gt;
              &lt;/EditItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField ShowHeader="False"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:LinkButton ID="editButton" runat="server" CommandName="Edit" Text="Edit"&gt;&lt;/asp:LinkButton&gt;
              &lt;/ItemTemplate&gt;
              &lt;EditItemTemplate&gt;
                  &lt;asp:LinkButton ID="updateButton" runat="server" CommandName="Update" Text="Update"&gt;&lt;/asp:LinkButton&gt;
              &lt;/EditItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
      &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>Code behind:</p>
  
  <pre><code>// This method works fine, no obvious problems here.
  protected void gridView_DataBound(object sender, EventArgs e)
  {
      // Loop through the Holidays that are bound to the GridView
      var holidays = (IEnumerable&lt;Holiday&gt;)gridView.DataSource;
      for (int i = 0; i &lt; holidays.Count(); i++)
      {
          // Get the row the Holiday is bound to
          GridViewRow row = gridView.Rows[i];
          // Get the PlaceHolder control
          var placeHolder = (PlaceHolder)row.FindControl("countriesPlaceHolder");
          // Create a CheckBox for each country and add it to the PlaceHolder
          foreach (Country country in this.Countries)
          {
              bool isChecked = holidays.ElementAt(i).Countries.Any(item =&gt; item.ID == country.ID);
              var countryCheckBox = new CheckBox
              {
                  Checked = isChecked,
                  ID = country.Abbreviation + "CheckBox",
                  Text = country.Abbreviation
              };
              placeHolder.Controls.Add(countryCheckBox);
          }
      }
  }
  
  protected void gridView_RowEditing(object sender, GridViewEditEventArgs e)
  {
      // EXAMPLE: I'm expecting checkBoxControls to contain my CheckBoxes, but it's empty.
      var checkBoxControls = gridView.Rows[e.NewEditIndex].FindControl("countriesPlaceHolder").Controls;
  
      gridView.EditIndex = e.NewEditIndex;
      BindData();
  }
  
  protected void gridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {
      // EXAMPLE: I'm expecting checkBoxControls to contain my CheckBoxes, but it's empty.
      var checkBoxControls = ((PlaceHolder)gridView.Rows[e.RowIndex].FindControl("countriesPlaceHolder")).Controls;
  
      // This is where I'd grab the values from the controls, create an entity, and save the entity to the database.
  
      gridView.EditIndex = -1;
      BindData();
  }
  </code></pre>
  
  <p>This is the article that I followed for my data binding approach: <a href="http://www.aarongoldenthal.com/post/2009/04/19/Manually-Databinding-a-GridView.aspx" rel="nofollow">http://www.aarongoldenthal.com/post/2009/04/19/Manually-Databinding-a-GridView.aspx</a></p>