title: Gridview Row Events not firing in UserControl
body: <p>I've got a pretty strange problem with my usercontrols that I'm working with. I'm coding some user controls to be used in a DotNetNuke module.</p>
  
  <p>Basically I have a UserControl on my page that holds some controls and then there is a Placeholder where I am loading in a UserControl that has a GridView placed in it.</p>
  
  <p>Basically here is my page structure</p>
  
  <pre><code>&lt;asp:Panel runat="server" ID="pnlServiceType" Visible="false"&gt;
     &lt;uc:ServiceType runat="server" ID="ServiceType" /&gt;
  &lt;/asp:Panel&gt;
  </code></pre>
  
  <p>Within that user control are some form elements and then the following: 
      </p>
  
  <pre><code>&lt;asp:PlaceHolder runat="server" ID="phServices" /&gt;
  </code></pre>
  
  <p>That placeholder then has a user control added to it like so: </p>
  
  <pre><code>&lt;p class="header"&gt;&lt;asp:Label runat="server" ID="lblServiceHeader" Font-Bold="true" /&gt;&lt;/p&gt;
  
  &lt;asp:GridView runat="server" ID="gvServices" AlternatingRowStyle-BackColor="#eaeaea"  BorderStyle="None" GridLines="None"
  AutoGenerateColumns="false" CellPadding="6" Width="100%" EnableViewState="false" OnRowEditing="gvServices_RowEditing"&gt;
  </code></pre>
  
  <p>When the user picks something in the DropDownList, I'm clearing the controls out of the Placeholder, and re-adding just a single UserControl for the set of records for whatever type they've picked like so</p>
  
  <pre><code>        _serviceID = e.Value;
  
          //Clear the controls out of the placeholder
          phServices.Controls.Clear();
  
          //Reset the count of grids for the OnInit() method
          Session["GridCount"] = 0;
  
          if (e.Value != "NULL")
          {
              PopulateServicesGrid(_service.GetServicesByFormType(Convert.ToInt32(e.Value)));
          }
          else
          {
              PopulateServicesGrid(_service.GetServicesByClient(Convert.ToInt32(_client)));
          }
  </code></pre>
  
  <p>And the PopulateServicesGrid method: </p>
  
  <pre><code>    private void PopulateServicesGrid(List&lt;NOAService&gt; services)
      {
          //Creates a LINQ grouping based on the Billing Codes
          //Allows a super easy creation of grids based on the grouped billing codes
          var query = services.Select(service =&gt; service.BillingCode).Distinct();
          foreach (string code in query)
          {
              var servicesByCode = services.Where(service =&gt; service.BillingCode == code).ToList();
              ServicesGrid servicesGrid = LoadControl("~/DesktopModules/LEL Modules/NOA/ServicesGrid.ascx") as ServicesGrid;
              Label lblServiceHeader = servicesGrid.FindControl("lblServiceHeader") as Label;
              GridView gvServices = servicesGrid.FindControl("gvServices") as GridView;
  
              phServices.Controls.Add(servicesGrid);
              servicesGrid.ID = code;
              lblServiceHeader.Text = servicesByCode[0].FormTypeName;
              gvServices.DataSource = servicesByCode;
              gvServices.DataBind();
  
              Session["GridCount"] = phServices.Controls.Count;
  
          }
      }
  </code></pre>
  
  <p>And on my ServiceType UserControl, on the PageInit, I'm readding the ServiceGrid usercontrol so that my grids show up across postbacks and aren't lost from the Placeholder</p>
  
  <pre><code>    void NOAServiceType_Init(object sender, EventArgs e)
      {
          for (int i = 0; i &lt; Convert.ToInt32(Session["GridCount"]); i++)
          {
              ServicesGrid servicesGrid = LoadControl("~/DesktopModules/LEL Modules/NOA/ServicesGrid.ascx") as ServicesGrid;
              phServices.Controls.Add(servicesGrid);
  
          }
      }
  </code></pre>
  
  <p>The grids populate successfully and everything seems to work just fine. But for some reason, on my GridView, I have a CommandField of </p>
  
  <pre><code>    &lt;asp:CommandField ShowEditButton="true" ItemStyle-HorizontalAlign="Right" EditText="Edit" UpdateText="Update" 
          EditImageUrl="~/images/LELModules/appbar.edit.rest.png" CancelImageUrl="~/images/LELModules/appbar.close.rest.png" UpdateImageUrl="~/images/LELModules/appbar.check.rest.png"
          ButtonType="Image" CausesValidation="false" /&gt;
  </code></pre>
  
  <p>When I click my Edit command on the Grid row, nothing happens. My grid doesn't lose its rows, the control is still there, everything seems like it should be ok. The <code>RowEditing</code> event doesn't fire until I click it a second time.</p>
  
  <p>Any idea why this might be occuring?</p>
  
  <p><strong>UPDATE</strong>: I've managed to figure out that my SelectedIndexChanged handlers are effectively resetting the <code>DataSource</code> on the Grid contained by the UserControl when they are readded to the PlaceHolder. When the CommandField (Edit) is clicked though, the <code>Init</code> fires for the UserControl that holds the placeholder</p>
  
  <pre><code>ServiceType UserControl &lt; `Init` fires here
     -Form elements
     -Placeholder which holds
        --UserControl with GridView
  </code></pre>
  
  <p>The <code>Init</code> method loads up new instances of the UserControl and adds them to the PlaceHolder, but the DataSource is <code>null</code>. With <code>EnableViewState=true</code> it looks like the data is still bound, but if I handle <code>PreRender</code>, I can see that the <code>DataSource</code> on my <code>gvServices | null</code></p>
  
  <p>Is it even possible to edit rows like this on a GridView that is being added dynamically to a PlaceHolder over and over?</p>
  
  <p><strong>FIXED</strong> I found out what the issue was after referring to this article 
      <a href="http://www.west-wind.com/weblog/posts/2006/Feb/24/Overriding-ClientID-and-UniqueID-on-ASPNET-controls" rel="nofollow">http://www.west-wind.com/weblog/posts/2006/Feb/24/Overriding-ClientID-and-UniqueID-on-ASPNET-controls</a></p>
  
  <p>It got me thinking, what if the IDs were getting changed? The controls are way nested. So I went and put a watch on the GridView's ID, ClientID, and UniqueID just to see. When the Control is loaded on my <code>Init</code> handler, it's assigned a super generic ID when it's added.</p>
  
  <pre><code>private void PopulateServicesGrid(List&lt;NOAService&gt; services)
  {
      //Creates a LINQ grouping based on the Billing Codes
      //Allows a super easy creation of grids based on the grouped billing codes
      var query = services.Select(service =&gt; service.BillingCode).Distinct();
      foreach (string code in query)
      {
          var servicesByCode = services.Where(service =&gt; service.BillingCode == code).ToList();
          ServicesGrid servicesGrid = LoadControl("~/DesktopModules/LEL Modules/NOA/ServicesGrid.ascx") as ServicesGrid;
          Label lblServiceHeader = servicesGrid.FindControl("lblServiceHeader") as Label;
          GridView gvServices = servicesGrid.FindControl("gvServices") as GridView;
  
          phServices.Controls.Add(servicesGrid);
          **servicesGrid.ID = code;**
          lblServiceHeader.Text = servicesByCode[0].FormTypeName;
          gvServices.DataSource = servicesByCode;
          gvServices.DataBind();
  
          Session["GridCount"] = phServices.Controls.Count;
  
      }
  }
  </code></pre>
  
  <p>I was setting the ID as something else. So when I was hitting the Edit RowCommand on my grid, it was reloading the controls again on <code>Init</code> and the IDs were being changed back from my custom set code (T2020, pulled from my database) to the generic ID again, it didn't know how to fire the event.</p>
  
  <p>I hope this helps someone as I've lost at least 12 hours fixing this problem.</p>