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

Recommended Answers

All 6 Replies

MODIFY THE BLOW LINE OF CODE:

OnClientClick="return confirm_delete()"

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.

As said by Saion, Thats also one option

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

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?

READ FUNCTION BEHAVIOUR.

MARK THE THREAD AS SOLVED. If your problem resolved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.