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


Top 5 Contributors of the Month
david stephan

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


        
            
            
            
            
                
                    
                
            
        
Cheers!!!
Pankaj

     

Further Readings:

Responses
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?");
}
routeEvent(e);
}
document.onclick = ConfirmDelete;

aspx code:

<asp:GridView RunAt="server" ID="gvTest" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server"
ID="DeleteMe" Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Eliza
http://www.mindfiresolutions.com/An-easy-way-to-bind-DELETE-confirmation-message-to-NET-controls-582.php
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