Sunday, December 02, 2007

asp.net : Add confirm delete to a GridView delete button

C#
OnClientClick='<%# "return confirm(\"do you want to delete " + Eval("Name") + "\")" %>'>


Visual basic

OnClientClick='<%# "return confirm(""do you want to delete " + Eval("Name") + """)" %>'>


using the onclientclick event of the button, linkbutton, or imagebutton control, we can add the confirm javascript function.


for each double quote closing or opening a string, we need to add the escape character \. The Eval function takes as a parameter whichever field name we need to display in the confirm section.


image


this technique has been around since .net 1.0, implemented in different ways. the onclientclick event is new in .net 2.0 and up, which makes it useful in this situation.

Comments are welcome

7 comments:

Anonymous said...

Many thanks

I wasted half a day trying to figure this out, that is using a confirm delete message with record info.

you're a genius! - JL

oudinia said...

Glad I could help. I have always searched for a simple and clear solution to the confirm delete technique (since .net 1.x). this works :) thanks

Anonymous said...

THANK YOU!!!!! that's what i was looking for...

oudinia said...

Glad this helps. Beleive it or not, I come back often to this post, during my develpopment, and do copy and paste :)
too bad, we cannot create code snippets for asp.net code
Good luck everyone!

S1n said...

Thank You Buddy!

Nice Trick, I was find for long time that.

Anonymous said...
This comment has been removed by a blog administrator.
Nehal Amrutiya said...

Good idea