Click here to Skip to main content
15,896,359 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using grid view for editing and deleting the user account in the asp.net having SQL server database. I have done updating the record in the database and deleting as well but i require to show a confirmation message before deleting the record into the database.

For this i used gridview_rowupdating event and gridview_rowdeleting event. Confirm message box is displayed but when i click OK button in the confirm message box it doesn't take to fire the event gridview_rowdeleting event. I am just thinking it doesn't call the event that is why i initialized onclientclick event for asp button to call the confirm messagebox and it is getting the output but when i click OK button not firing anything.

I will share the code please anybody explain me the situation to handle this case

What I have tried:

ASP Button:
<asp:Button ID="btnDelete" runat="server" Text="Delete" CssClass="btn btn-link text-center" ToolTip="Delete User Details" CommandName="Delete" UseSubmitBehavior="false" CausesValidation="false"                                                             OnClientClick="return ConfirmDeletion();"  OnClick="btnDelete_Click" />


Javascript:
function ConfirmDeletion() {
debugger;
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";

if (confirm("Are you sure you want to delete the record?") == true)
    confirm_value = "Yes";
else
    confirm_value = "No";

}


Delete Button Code Behind for sample:
protected void btnDelete_Click(object sender, EventArgs e)
{
    string confirmValue = Request.Form["confirm_value"];
    if (confirmValue == "Yes")
    {
        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!')", true);
    }
    else
    {
        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!')", true);
    }
}
Posted
Updated 16-Mar-18 6:25am
Comments
Member 8583441 16-Mar-18 7:26am    
I got the solution and now it's working
F-ES Sitecore 16-Mar-18 7:36am    
One thing you're not doing (there might be more) is returning anything from your ConfirmDeletion function. How does the page know if the function is saying to proceed or not? You need to return true or false

if (confirm("Are you sure you want to delete the record?") == true)
{
confirm_value = "Yes";
return true;
}
else
{
confirm_value = "No";
return false;
}

1 solution

Try:
OnClientClick="if(!ConfirmDeletion()){return false;}"

Quite often, the client-side click event handler will have extra code added to perform a post-back. By adding an unconditional return at the start, you prevent that code from running whichever option the user picks.
 
Share this answer
 
Comments
Member 8583441 17-Mar-18 0:37am    
Thanks a lot for giving me the solution

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