Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 13th, 2006, 09:08 AM
Friend of Wrox
 
Join Date: Oct 2005
Location: , , United Kingdom.
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
  #2 (permalink)  
Old July 18th, 2006, 12:35 PM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
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

  #3 (permalink)  
Old July 18th, 2006, 01:16 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 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
  #4 (permalink)  
Old July 19th, 2006, 09:28 AM
Friend of Wrox
 
Join Date: Oct 2005
Location: , , United Kingdom.
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
Default

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

Rit
  #5 (permalink)  
Old August 31st, 2006, 12:19 PM
Registered User
 
Join Date: Aug 2006
Location: , Tennessee, .
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


  #6 (permalink)  
Old March 8th, 2007, 06:47 PM
Registered User
 
Join Date: Mar 2007
Location: , , .
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.

 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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 hardik@saltriver.com 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



All times are GMT -4. The time now is 11:57 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.