title: GridView DataKeys Collection Empty When GridView is in an EditItemTemplate
body: <p>All,</p>
  
  <p>FYI: I am using VS2005, .net 2.0.</p>
  
  <p>I have a GridView control that exists in the FormView EditItemTemplate. Unfortunately, the GridView misbehaves in that setup, its DataKeys collection is empty when the page posts back and the select command of the gridview fires.</p>
  
  <p>Here is the sequence of events:</p>
  
  <ol>
  <li>User browses to the page</li>
  <li>User clicks 'edit' (FormView renders EditItemTemplate where the GridView is)</li>
  <li>The user clicks search which postbacks again and populates the GridView located in the EditItemTemplate (at this point the GridView has DataKeys)</li>
  <li>User selects item from GridView which raises row selected event</li>
  <li>On postback, the GridView RowCommand fires,- I check that the CommandName = "select" and run my code which throws exception because the DataKeys collection is empty at this point!</li>
  </ol>
  
  <p>One more additional detail: The GridView lives in a user control that resides in the EditItemTemplate of the FormView.</p>
  
  <p>NOTE:</p>
  
  <p>I was able to resolve this problem when I move my GridView out of the EditItemTemplate of the FormView,- now DataKeys collection is NOT empty. Unfortunately, the GridView has to be in the EditItemTemplate for the user to select clients he searched for.</p>
  
  <p>Any insight would be appreciated.</p>
  
  <p>Event handling code:</p>
  
  <pre><code>protected void ctlSearchResults_RowCommand(object sender, GridViewCommandEventArgs e)
  {
      // user has selected the client from the keyword search result list
      if (e.CommandName == "select")
      {
          GridView searchResultsGrid = (GridView)e.CommandSource;
          int selectedRowIndex = int.Parse((string)e.CommandArgument);
          int clientId = (int)searchResultsGrid.DataKeys[selectedRowIndex][Client.PROP_ENTITYID];
          // raise Selected event
          _OnSelected(new ClientSelectedEventArgs(clientId));
      }
  }
  </code></pre>