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.
My grid is wrapped in an update panel that causes the postback. My delete button is only inside my grid as the template column, one for each row. Before the method below is called, I am calling another method to actually remove the rows from my DataTable whose IDs have been deleted on the client via LINQ. That method is not the problem, as it has been used in multiple scenarios. Once my DataTable rows have been updated with row state 'Deleted,' the table is passed to the method right here, where as you can see I'm attempting to clear everything out before rebinding.
I've also thrown in the markup for my delete button for good measure. I am not using submit behavior on this button in order to identify it in the EVENTTARGET.
private void BindViewGroupsGrid(DataTable tblViewGroups){ grdViewGroups.ClearDataSource(); //grdViewGroups.Rows.Clear(); grdViewGroups.DataSource = tblViewGroups; if (!IsPostBack || Request.Form["__EVENTTARGET"] == btnAddViewGroup.UniqueID || Request.Form["__EVENTTARGET"].Contains("btnDelete")) { grdViewGroups.Rows.Clear(); grdViewGroups.DataBind(); }}<ig:TemplateDataField Key="TemplateField_2" Width="50px"> <ItemTemplate> <asp:Button ID="btnDelete" OnClientClick="onDelete_Click();" runat="server" Text="Delete" CssClass="FloatRight" UseSubmitBehavior="False" /> </ItemTemplate></ig:TemplateDataField>
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.
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.
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.