Controlling whether or not the user can delete a GridView row
Home > ASP tutorials > Row Deleting
This article describes how I implemented a GridView in ASP.NET that only allows you to
delete rows from it that meet certain criteria. If they don't meet the criteria,
an error message pops up.
The special delete buttons in the GridView
I had a GridView, and for each row I'd set it up to have edit and delete buttons
like this:

I now wanted to make it so that the delete button didn't simply delete the row,
but first looked in the database to see if it is ok to delete the row, and cancel
the delete operation if it is not ok. I mean, you can't just delete a product
from your database if it features on one of your client's orders, can you? You've
got to look in the database, check it isn't going to ruin your data integrity,
then do the delete if things are ok.
Also, I wanted it to popup a warning box to say "Sorry, you can't delete
that item as it is used by this record: xyz". The only way I could get
it to do that was to use JavaScript.
How to implement the GridView
Here is a two-point sumary of what I did:
- I made it so that when the GridView is constructed, each Delete button is
customized. As each row of the GridView is built, a database lookup is done
to see if the row is deletable or not. If not, the Delete button gets its
onclick event set to trigger a Javascript alert box saying why it can't be
deleted.
- Then when a Delete button is clicked, I used the RowDeleting event, look
at the Delete button for this row, and see if it has an onclick event set
up for it. If not, fine, let ASP.NET delete the row. If it has, block the
delete and let the JavaScript alert box fire off.
This is what you need to do:
- First, create an event handler for the GridView RowDataBound
event. We are going to modify the Delete button in each row as it is databound.
This way we can link a javascript alert popup to the delete button in each
row, if the row can't be deleted.
- In RowDataBound, make sure that we are actually doing a data row here, not
a heading, by using:
if (e.Row.RowType == DataControlRowType.DataRow)
- Find the Delete button for this row using something like:
Button delete = (Button)e.Row.Cells[0].Controls[2];
- Do an SQL query to test if we can delete this row or not. I got hold of
the row id by using:
string part_id = e.Row.Cells[1].Text;
- If we find that we can't delete the row, add the onclick event like this:
delete.Attributes.Add("onclick", "alert('You cannot delete
this row')");
- Now link up the RowDeleting event for the GridView. When
a RowDeleting event is called, we can find the Delete button in the row by
using:
Button delete = (Button)GridView2.Rows[e.RowIndex].Cells[0].Controls[2];
- Then, if we find that the delete button has an onclick event, just set e.Cancel=true;
The parameter e is passed with the RowDeleting event, and setting e.Cancel=true
stops the row from being deleted. You can check if the onclick attribute is
set by using something like:
string isitset=delete.Attributes[("onclick")];
The onclick event will fire and the alert box will appear.
If we don't set e.cancel = true, then e.cancel remains false, and the row
gets deleted for us by the inner workings of ASP.NET.
And that is pretty much it.
Read more ASP tutorials
Last updated 29th August 2006
|