Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i have put autogeneratDelete button in gridview.i have to apply javascript on this link.Ex: when i click on delete link dialog box open and ask for confirm to delete.
i have try this,but it is not working properly.when i click first time it will not open dialog box ,when i click second time dialog box is open but when i click on cancel ,then also data will be deleted.

GridView1.Attributes.Add("onclick", "javascript:confirmDelete('Server ID')");


i have put javascript in .js file.
Posted

You need to add this script for every row. For this you need to apply this at RowDataBound event of Gridview.
There are a lots details on it if you search google.

You can refer
GridView Delete, with Confirmation[^]
 
Share this answer
 
Please make sure your have wrote your codes to delete any row inside the following if block

C#
if(confirm(".....")){
}


This will prevent deletion when user press cancel.


Also if you can post your code in this forum so it will be more easy for me to debug this.
 
Share this answer
 
v3
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{   
if (e.Row.RowType == DataControlRowType.DataRow)   
{      
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)      
{             
((LinkButton)e.Row.Cells[0].Controls[2]).Attributes["onclick"] = "if(!confirm('Are you sure you want to delete this record?'))return  false;";      
}    
} 
}
 
Share this answer
 
C#
function confirmDelete(){
                    var userResponse = confirm('Do you want to delete this permanently?');
                    if (userResponse ==true){
                        detachValidationEngine();
                    }
                    return userResponse;
                }

Call it on client click
OnClientClick="return confirmDelete();"
 
Share this answer
 
Hello

Use this

GridView1.Attributes.Add("OnClientClick", "javascript:return confirm('Are you certain you want to delete this 
product?')");




also you can used button insted og autoDeleteButton


XML
<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False"
    CommandName="Delete" Text="Delete"
    OnClientClick="return confirm('Are you certain you want to delete this
product?');">
</asp:LinkButton>




For More Help
http://msdn.microsoft.com/en-us/library/ms972940.aspx[^]


http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.autogeneratedeletebutton.aspx[^]



i hope your problem will solve if not please give me your Comment
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900