Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 13th, 2006, 09:08 AM
Friend of Wrox
 
Join Date: Oct 2005
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
Default Delete Confirmation From within a Gridview

Hi All

I am using the ShowDeleteButton within a Gridview and the Gridiew is bound to an SQLDataSource control that houses the Delete command. I wondered what would be the best way to provide the user with a confirmation alert box before the record is deleted to make sure they really want to delete that particular record. Also it helps to stay clear of accidental deletions too.

What would be the best way to tackle this?

Many thanks

Rit
__________________
Rit
www.designandonline.co.uk
INSPIRE | CREATE | DELIVER
 
Old July 18th, 2006, 12:35 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Use the rowdatabound event to add an attribue to the linkbutton("Select"), assuming the linkbutton is in the first column of the gridview:

If e.Row.RowType = DataControlRowType.DataRow Then
   Dim lb As LinkButton
   lb = e.Row.Cells(0).Controls(0)
   lb.Attributes.Add("onclick", "javascript:return confirm('Are you sure you want to delete this row?');")
End If

Jim

 
Old July 18th, 2006, 01:16 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

What the solution that Jim proposed should generally work, there are easier ways to do this.

On the LinkButton, you can add an OnClientClick directly, with the following code:

OnClientClick="return confirm('Are you sure you want to delete
          this item?');"

However, to make that work, you'll need to convert the Delete button into a Template field. The following post gives more details about this:

http://p2p.wrox.com/topic.asp?TOPIC_ID=45586

It also gives an answer to why under certain circumstances your confirmation button no longer seems to delete stuff.

One final note: you don't need the javascript: prefix in Jim's code. The onclick already indicates that script should be fired.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old July 19th, 2006, 09:28 AM
Friend of Wrox
 
Join Date: Oct 2005
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
Default

THats brilliant, Many thanks, I'll explore both options.

Rit
 
Old August 31st, 2006, 12:19 PM
Registered User
 
Join Date: Aug 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The row databound example you gave is exactly what I am looking for. However, I am having a slight problem with it. I have a gridview that is generating the Edit and Delete functions. When I insert your code into the row databound event, it fires on the Edit link instead of the Delete. I thought that since the Edit button is listed first, the index of the Delete link inside of the cell would be 1. This is not work. Below is what I tried. Any suggestions?

If e.Row.RowType = DataControlRowType.DataRow Then
   Dim lb As LinkButton
   lb = e.Row.Cells(0).Controls(1)
   lb.Attributes.Add("onclick", "javascript:return confirm('Are you sure you want to delete this row?');")
End If


 
Old March 8th, 2007, 06:47 PM
Registered User
 
Join Date: Mar 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This works.

If e.Row.RowType = DataControlRowType.DataRow Then
   Dim lb As LinkButton
   lb = e.Row.Cells(0).Controls(0)
   lb.Attributes.Add("onclick", "if(!confirm('Are you sure you want to delete this row?') return false;")
End If

You can use the field template as others have suggested but this is much more simple and will fire the correct event.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Confirmation with checkbox in gridview collie Visual Studio 2005 0 October 24th, 2007 06:13 AM
confirmation from user from a delete button in the sansircar ASP.NET 1.0 and 1.1 Professional 1 January 25th, 2006 01:44 PM
Confirmation message on delete of Data grid item [email protected] VS.NET 2002/2003 1 December 22nd, 2004 02:27 AM
delete confirmation yami56 Access 2 April 7th, 2004 12:09 PM
Pop up Delete confirmation box... developerz BOOK: ASP.NET Website Programming Problem-Design-Solution 1 October 8th, 2003 01:41 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.