All,
FYI: I am using VS2005, .net 2.0.
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.
Here is the sequence of events:
- User browses to the page
- User clicks 'edit' (FormView renders EditItemTemplate where the GridView is)
- The user clicks search which postbacks again and populates the GridView located in the EditItemTemplate (at this point the GridView has DataKeys)
- User selects item from GridView which raises row selected event
- 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!
One more additional detail: The GridView lives in a user control that resides in the EditItemTemplate of the FormView.
NOTE:
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.
Any insight would be appreciated.
Event handling 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));
}
}