title: Template field disappears
body: <p>I have a gridview used to display records based on a selected value in a drop down.</p>
  
  <p>Within the gridview, there are a dynamic number of bound field columns added in code behind, followed by a drop down list in a template field and a button in a second template field.</p>
  
  <p>The problem is, when I click on the button, the template fields disappear.</p>
  
  <p>GRID VIEW</p>
  
  <pre><code>&lt;asp:UpdatePanel ID="upnlDetail" runat="server" UpdateMode="Conditional"&gt;
      &lt;ContentTemplate&gt;
          &lt;asp:GridView ID="gvDetails" runat="server"
              AutoGenerateColumns="false" 
              SkinID="gridviewGray" 
              CellPadding="3" 
              OnRowCommand="gvDetails_RowCommand" 
              OnRowDataBound="gvDetails_RowDataBound" 
              AllowSorting="true"&gt;
              &lt;Columns&gt;
                  &lt;asp:TemplateField&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:DropDownList ID="ddlStatus" runat="server"
                              AutoPostBack="true" 
                              OnSelectedIndexChanged="ddlStatus_SelectedIndexChanged"&gt;
                          &lt;/asp:DropDownList&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
                  &lt;asp:TemplateField&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:Button ID="btnSave" runat="server"
                              CommandName="Save" 
                              CommandArgument="&lt;%# ((GridViewRow) Container).RowIndex %&gt;" 
                              Text="Save" /&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
              &lt;/Columns&gt;
          &lt;/asp:GridView&gt;                                
      &lt;/ContentTemplate&gt;
  &lt;/asp:UpdatePanel&gt;
  </code></pre>
  
  <p>CODE BEHIND</p>
  
  <pre><code>while (gvDetails.Columns.Count &gt; 2)  //Don't remove the rightmost columns
  { 
      gvDetails.Columns.RemoveAt(0);
  }
  
  gvDetails.Columns.Insert(0, GridViewTools.CreateBoundField(
                                  "Request.Amount", "Amount", "Money", 50));
  gvDetails.Columns.Insert(0, GridViewTools.CreateBoundField(
                                  "Request.CustomerName", "Customer", "comment", 200));
  gvDetails.Columns.Insert(0, GridViewTools.CreateBoundField(
                                  "Request.BillAccountFormatted", "Account", "text", 100));
  gvDetails.Columns.Insert(0, GridViewTools.CreateBoundField(
                                  "Request.Id", "Request", "int", 50));
  
  gvDetails.DataSource = dt;
  gvDetails.DataBind();
  </code></pre>
  
  <p>Method GridViewTools.CreateBoundField is a custom method that sets my default boundfield attributes and returns the BoundField object. </p>
  
  <pre><code>public static BoundField CreateBoundField(...){...}
  </code></pre>
  
  <p>In testing it, the gridview populates as desired on the initial load.  However, the RowCommand event does not fire and both template fields disappear when the button is clicked.  The next gridview.RowDataBound event then throws a null object error because the drop down list is no longer found in the row.  </p>
  
  <p>If I remove lines adding the bound fields [Columns.Insert], the RowCommand fires as expected when btnSave is clicked and the template fields persist.  If I add even a single bound field column again, the RowCommand does not fire and the template fields disappear.</p>
  
  <p>Any suggestions?  Why would adding a new column to a gridview invalidate the RowCommand event for a pre-existing button and make the template fields disappear?</p>
  
  <p><strong>UPDATES</strong>
  I've removed the template fields from the Grid View declaration and added the controls to a bound field instead, but still get the same result.  The show on the initial load, but disappear once clicked.  </p>
  
  <p>I've also looked at the grid view in the Page_Unload event, and it still contains the columns at that point, even though they don't show on screen.  Is there any event I can capture after Page_Unload?</p>