I am using a WebDataGrid with a template column containing an ASP.NET button. This button is used to delete a row from my grid by storing the row's data key in my hidden field hfDeletedViewGroupKeys, which I then parse on the server side to remove all rows from a datatable whose IDs appear in this field. I then rebind the table data to my grid after any time the delete button is clicked.
Here is my problem. My onRowSelectionChanged() retrieves the correct data key if I move around from row to row clicking each one's respective delete button. However, if I stay on one row and delete it, all the other rows are "bumped up" so a new row occupies the position of the one I just deleted. If I click on that new row in the same position as the one just deleted, most of the time, the data key retrieved in my client script is the old one. The effect is that the row is not actually deleted until I click on a row somewhere and then reclick that row.
Could someone enlighten me as to what I am doing wrong with this approach?
var selectedViewGroupKey = ""; function onRowSelectionChanged(sender, eventArgs) { var rows = eventArgs.getSelectedRows(); var selectedRow = rows.getItem(0); selectedDataKey = selectedRow.get_dataKey(); var hfSelectedDataKey = document.getElementById("<%= hfSelectedViewGroupKey.ClientID %>"); hfSelectedDataKey.value = selectedDataKey + ",";} function onDelete_Click() { var hfDeletedDataKeys = document.getElementById("<%= hfDeletedViewGroupKeys.ClientID %>"); var hfSelectedDataKey = document.getElementById("<%= hfSelectedViewGroupKey.ClientID %>");
hfDeletedDataKeys.value += hfSelectedDataKey.value;
}
Hello bgl3317,
Let us know if you have further questions regarding this.
Have you set DataKeyFields of the WebDataGrid,
if you have not the grid takes the row position in the WebDataGrid.
<ig:WebDataGrid ID="WebDataGrid1" runat="server" Height="100%" Width="100%" DataSourceID="SqlDataSource1"
OnDataFiltered="WebDataGrid1_DataFiltered" DefaultColumnWidth="400px" DataKeyFields="EmployeeID">
And getting the DataKey in the JS finction:
selectedDataKey = selectedRow.get_dataKey()[0];
This explains the issue why the data key is the same when the new row at the
same position retrieves the old one.
You should clear the DataSource, when you change it.
this.WebDataGrid1.ClearDataSource();
Also you should call SelectedRows.Clear() afterwards :
this.WebDataGrid1.Behaviors.Selection.SelectedRows.Clear();
Please let me know if you need further assistance regarding this.
If you wanted to have some fun to test the timing and possibly fix the issue, maybe make a loop in your javascript to keep trying to add the datakey to your hidden field but it should check first if it's there already. If it's there don't add it. See if it works after a few seconds or not.
Good luck, Ed
I have, all is good there. It must be a timing thing, because when I'm debugging the issue and returning to the web form after some delay, each new row occupying the current selected row index is deleted properly. It's kind of frustrating that there's not something more legitimate than this causing the problem.
Thanks again for offering your help though. I really appreciate it.
Hi,
Have you tried a breakpoint after the databind to see if the deleted row is still there at that point? I guess it's a question of timing or something wrong in the partial postback.
Ed