.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh
Melody Anderson
Eminent IT

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Display Confirm Message box on delete row in a gridview

Posted By:Pankaj Mishra       Posted Date: January 10, 2010    Points: 10    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

Display Confirm Message box on delete row records in a gridview with client side JavaScript.

If you want to display message box to the user on row deleting asking for the confirmation to delete records or not in a GridView, You can do this using one line  of javascript code. 

you can use javascript confirm() dialog for asking user confirmation to delete records or not?

Add this confirm() dialog onclientclick event of the asp.net button  control or any other control if you are using such as asp.net image button control or Link button.

Here I am using asp.net button control for deleting records and add below line of code in template column of GridView in side a button control

OnClientClick="return confirm('Do you want to delete this record?');"

Your GridView code will look like this



Further Readings:

Author: elizas         Company URL: http://www.dotnetspark.com
Posted Date: February 17, 2010

1. Here we have a function The 'ConfirmDelete()' which is executed each time a click event occurs on the page.
2. Then we use a logic that to find which element was clicked on i.s we append a word 'DELETE' to the ID of the control.
3.And finally check if the clicked element having ID is 'DELETED' , then the javascript function will be execute to ask the user about his/her confirmation.

JavaScript code:

function ConfirmDelete(e)
var targ;

if (!e)
var e = window.event;
targ = (e.target) ? e.target : e.srcElement;

if (targ.nodeType == 3)
targ = targ.parentNode;

if (targ.id.toLowerCase().indexOf("delete") >= 0)
return confirm("Do you want to delete this item?");
document.onclick = ConfirmDelete;

aspx code:

<asp:GridView RunAt="server" ID="gvTest" DataSourceID="SqlDataSource1">
<asp:LinkButton runat="server"
ID="DeleteMe" Text="Delete" />
Author: muthu         Company URL: http://www.dotnetspark.com
Posted Date: March 15, 2010

Nice post. Thank you.

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend