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;
}
Hi,
I would try using WebDataGrid.Rows.Clear before doing the databind. That would refill all the row data from scratch.
Ed
Thanks for the response Ed. I was trying to do something similar to your suggestion by calling WebDataGrid.ClearDataSource(). Neither that nor WebDataGrid.Rows.Clear is working at the moment. I'm wondering if this has something to do with the timing between the row changed event and my onDelete_Click() event that fires when I click the delete button within the row. Are there any other suggestions?
When you do the actual delete of the selected records, are you doing a full postback? Are you calling the delete function through an ASP button outside of the grid? Did you try the rows.clear in the code immediately following the delete function? If you have some server code to show, that would help.
Hello bgl3317,
Let us know if you have further questions 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.
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.
To clarify, I am deleting rows in my DataTable utilizing LINQ. It knows which rows to delete based on the hidden field that stores IDs marked for deletion by clicking the button on the client. This of course only doesn't work when it can't get the updated data key if I attempt to delete a row that replaces the row position of a previously deleted row in my grid.