I have an html table with id:#mytable
in my asp.net-4.5 webpage, which is created in the c# site and sending to the aspx. In this Html table, at the end of each row there is a DELETE
button like below:
...
html.Append("<td><input type=\"button\" runat=\"server\" value=\"Delete\" onclick=\"deleteRow(this)\" /></td>");
When this button is clicked, deleteRow(this) function is triggered with the code below:
btn.parentNode.parentNode.parentNode.removeChild(row);
When this line above runs, the regarding row is being immediately deleted and I do not see the row in the screen anymore.
This html table is represented as a datatable (javascript) like below:
$('#myTable').on('error.dt', function (e, settings, techNote, message) {
console.log('An error has been reported by DataTables: ', message);
}).DataTable({ fixedColumns: true,
"columnDefs": [{
"width": '60%',
"defaultContent": "-",
"targets": "_all"
}]
});;
This is also working well, my html table turns into a datatable. My problem is, after I delete a row from the table, if I change the sorting, or entry limit per page (10 by default) etc., I start to see the row I have just removed on the page, inside the table. It seems like my deleteRow function deletes the row not permanently. How can I fix this issue? Any help will be so appreciated.
EDIT: I needed to refresh the page after delete button click with JS: document.location.reload(true)
and page_load re-reads the data, to fix the issue. Thanks @NTR and @J E Carter II
EDIT2: Using $('#myTable').DataTable().rows(row).remove().draw(false);
while removing the row fixed the issue without the need of re-read data from DB. Check my answer.