954,577 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Asp.net javascript confirmation box not cancelling

Hi, i have been trying to use the javascript confirmation box to delete files from a Gridview, however when ever i click "cancel" on the box it still deletes the file! i really need some help, heres the code i have:

<asp:TemplateField HeaderText="Delete File">
                            <ItemTemplate>
                                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" CommandName="Delete" 
                                    OnClick="DeleteRowButton_Click" OnClientClick="confirm_delete()">
         Permanently Delete This File</asp:LinkButton>
                            </ItemTemplate>


<script type="text/javascript">
    function confirm_delete() {
        if (confirm("Are you sure you want to delete the highlighted file? ") == true)
            return true;
        else
            return false;

    }
     </script>


//Delete a selected file from the Server and Gridview
void DeleteRowButton_Click(Object sender, EventArgs e)
        {
            
                //Delete file from the server
                String fileName = GridView1.SelectedRow.Cells[1].Text;

                String filePath = "/UploadedUserFiles/" + User.Identity.Name + "/" + fileName;

                System.IO.File.Delete(Server.MapPath(filePath));

                lblDeleteResult.Text = fileName + " was successfully deleted!";

                // Programmatically delete the selected record from the gridview
                GridView1.DeleteRow(GridView1.SelectedIndex);
                //Refresh Gridview
                GridView1.DataBind();

                UploadDetails.Visible = false;
                          
        }


basically the DeleteRowButton_Click method runs regardless of whether i click cancel. i would like it to run this method if i click "ok" and do nothing when a click "cancel"

any ideas?
thanks

julseypart
Light Poster
42 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

MODIFY THE BLOW LINE OF CODE:

OnClientClick="return confirm_delete()"
mail2saion
Posting Whiz in Training
247 posts since Apr 2009
Reputation Points: 26
Solved Threads: 44
 

In case "OK" is clicked then return true else when "Cancel" is clicked then return false which will not submit your page and the row will not get deleted.

mahendrabilla
Newbie Poster
18 posts since Apr 2009
Reputation Points: 17
Solved Threads: 1
 

As said by Saion, Thats also one option

mahendrabilla
Newbie Poster
18 posts since Apr 2009
Reputation Points: 17
Solved Threads: 1
 

Please define Javascript onClientClick function as below
OnClientClick="return confirm_delete()"
And that should resolve your issue.

Hi, i have been trying to use the javascript confirmation box to delete files from a Gridview, however when ever i click "cancel" on the box it still deletes the file! i really need some help, heres the code i have:

<asp:TemplateField HeaderText="Delete File">
                            <ItemTemplate>
                                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" CommandName="Delete" 
                                    OnClick="DeleteRowButton_Click" OnClientClick="confirm_delete()">
         Permanently Delete This File</asp:LinkButton>
                            </ItemTemplate>


<script type="text/javascript">
    function confirm_delete() {
        if (confirm("Are you sure you want to delete the highlighted file? ") == true)
            return true;
        else
            return false;

    }
     </script>


//Delete a selected file from the Server and Gridview
void DeleteRowButton_Click(Object sender, EventArgs e)
        {
            
                //Delete file from the server
                String fileName = GridView1.SelectedRow.Cells[1].Text;

                String filePath = "/UploadedUserFiles/" + User.Identity.Name + "/" + fileName;

                System.IO.File.Delete(Server.MapPath(filePath));

                lblDeleteResult.Text = fileName + " was successfully deleted!";

                // Programmatically delete the selected record from the gridview
                GridView1.DeleteRow(GridView1.SelectedIndex);
                //Refresh Gridview
                GridView1.DataBind();

                UploadDetails.Visible = false;
                          
        }

basically the DeleteRowButton_Click method runs regardless of whether i click cancel. i would like it to run this method if i click "ok" and do nothing when a click "cancel"

any ideas? thanks

HarshKuntail
Newbie Poster
1 post since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

yes, its worked! i tryed for 2 hours yesterday doing all kinds of wierd complicated stuff, thanks!

why did the "return" make such a big difference?

julseypart
Light Poster
42 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

READ FUNCTION BEHAVIOUR.

MARK THE THREAD AS SOLVED. If your problem resolved.

mail2saion
Posting Whiz in Training
247 posts since Apr 2009
Reputation Points: 26
Solved Threads: 44
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You