The first step to allow users to delete a row from the
ListView is to provide them with a Delete Link / Delete button for every row in
the ListView. This can be done by adding the following to the ItemTemplate
collections for the ListView
<asp:LinkButton ID="lnkDelete" CommandName="Delete" runat="server" Text="Delete" />
The command name should be set to “Delete”. That is the key
here. Now, we have something for the user to click so that the user can delete
the row. When the Delete link is clicked, the ItemDeleting event will be fired.
We could invoke a method for this event. Within the method we could get the
value for the primary key and invoke a stored procedure or use an inline SQL
Display a warning message when the user clicks Delete
in a ListView Control?
It will be always helpful to ask user before they actually
delete the row from the ListView and eventually from the database. We could
display a warning message whether they really intend to delete the row. If the
reply is either yes or OK, then we could carry out with the actual removal of
the rows from the database.
This can be done using the following technique:
During the ItemDataBound event, we can attach a client side
event for each Delete Link in the ListView control.
protected void DataBoundList(Object sender, ListViewItemEventArgs e)
if (e.Item.ItemType == ListViewItemType.DataItem)
LinkButton lnkDelete = (LinkButton)e.Item.FindControl("lnkDelete");
if (lnkDelete != null)
"return confirm('Are you sure you want to delete this author?');");
Clicking on the delete link will result in the following
alert box. If the user clicks cancel, then the delete operation will not be
Figure 4: ListView Delete Confirmation message
Through out this article, we saw many methods which were
invoked for many events. The following EventHandlers will be helpful. Basically
these event handlers raise the corresponding events. It is through these
methods we were able to customize the ListView output.
<asp:ListView ID="ListView1" runat="server"