Brian's Blog

items I see across my tribes

DetailsView Confirm Delete

January 07
by briancarter 7. January 2009 21:32

Confirm Delete on ASP.NET DetailsView

I have been asked several times how to implement a Confirm Delete on the delete button of a DetailsView. Here is how you do it...
 
Put this in the “DetailsView_ItemCreated” event then replace the "DetailsView2" with your DetailsView ID and it should work fine.  Make sure you add the event onto your DetailsView and if the command bar is not the last element in the rows collection - you will need to update the index.
// Test FooterRow to make sure all rows have been created
        if (DetailsView2.FooterRow != null)
        {
            // The command bar is the last element in the Rows collection
            int commandRowIndex = DetailsView2.Rows.Count - 1;
            if (commandRowIndex > 0)
            {
                DetailsViewRow commandRow = DetailsView2.Rows[commandRowIndex];
                // Look for the DELETE button
                DataControlFieldCell cell = (DataControlFieldCell)commandRow.Controls[0];
                foreach (Control ctl in cell.Controls)
                {
                    LinkButton link = ctl as LinkButton;
                    if (link != null)
                    {
                        if (link.CommandName == "Delete")
                        {
                            link.ToolTip = "Click here to delete";
                            link.OnClientClick = "return confirm('Do you really want to delete this record?');";
                        }
                    }
                }
            }
        }

Categories: Development


 Questions or Feedback, my contact information is located on my About page.


The opinions, thoughts, and comments made in these blog posts are solely my own (unless otherwise stated). They do not reflect the opinions, thoughts or practices of my employer, my universities, my family, or anyone else. Also, I retain the right to change my mind about anything I publish here without having to go back and edit posts that occurred in the past. 

These are my opinions, or just as likely, someone else's opinions that I leveraged for my own.