943,865 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 2810
  • ASP.NET RSS
Apr 20th, 2009
0

Asp.net javascript confirmation box not cancelling

Expand Post »
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:

c# Syntax (Toggle Plain Text)
  1. <asp:TemplateField HeaderText="Delete File">
  2. <ItemTemplate>
  3. <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" CommandName="Delete"
  4. OnClick="DeleteRowButton_Click" OnClientClick="confirm_delete()">
  5. Permanently Delete This File</asp:LinkButton>
  6. </ItemTemplate>
  7.  
  8.  
  9. <script type="text/javascript">
  10. function confirm_delete() {
  11. if (confirm("Are you sure you want to delete the highlighted file? ") == true)
  12. return true;
  13. else
  14. return false;
  15.  
  16. }
  17. </script>
  18.  
  19.  
  20. //Delete a selected file from the Server and Gridview
  21. void DeleteRowButton_Click(Object sender, EventArgs e)
  22. {
  23.  
  24. //Delete file from the server
  25. String fileName = GridView1.SelectedRow.Cells[1].Text;
  26.  
  27. String filePath = "/UploadedUserFiles/" + User.Identity.Name + "/" + fileName;
  28.  
  29. System.IO.File.Delete(Server.MapPath(filePath));
  30.  
  31. lblDeleteResult.Text = fileName + " was successfully deleted!";
  32.  
  33. // Programmatically delete the selected record from the gridview
  34. GridView1.DeleteRow(GridView1.SelectedIndex);
  35. //Refresh Gridview
  36. GridView1.DataBind();
  37.  
  38. UploadDetails.Visible = false;
  39.  
  40. }

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
Last edited by julseypart; Apr 20th, 2009 at 3:18 am.
Reputation Points: 10
Solved Threads: 0
Light Poster
julseypart is offline Offline
42 posts
since Oct 2008
Apr 20th, 2009
0

Re: Asp.net javascript confirmation box not cancelling

MODIFY THE BLOW LINE OF CODE:
ASP.NET Syntax (Toggle Plain Text)
  1. OnClientClick="return confirm_delete()"
Reputation Points: 26
Solved Threads: 44
Posting Whiz in Training
mail2saion is offline Offline
247 posts
since Apr 2009
Apr 20th, 2009
0

Re: Asp.net javascript confirmation box not cancelling

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.
Reputation Points: 17
Solved Threads: 1
Newbie Poster
mahendrabilla is offline Offline
18 posts
since Apr 2009
Apr 20th, 2009
0

Re: Asp.net javascript confirmation box not cancelling

As said by Saion, Thats also one option
Reputation Points: 17
Solved Threads: 1
Newbie Poster
mahendrabilla is offline Offline
18 posts
since Apr 2009
Apr 20th, 2009
0

Re: Asp.net javascript confirmation box not cancelling

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

Click to Expand / Collapse  Quote originally posted by julseypart ...
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:

c# Syntax (Toggle Plain Text)
  1. <asp:TemplateField HeaderText="Delete File">
  2. <ItemTemplate>
  3. <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" CommandName="Delete"
  4. OnClick="DeleteRowButton_Click" OnClientClick="confirm_delete()">
  5. Permanently Delete This File</asp:LinkButton>
  6. </ItemTemplate>
  7.  
  8.  
  9. <script type="text/javascript">
  10. function confirm_delete() {
  11. if (confirm("Are you sure you want to delete the highlighted file? ") == true)
  12. return true;
  13. else
  14. return false;
  15.  
  16. }
  17. </script>
  18.  
  19.  
  20. //Delete a selected file from the Server and Gridview
  21. void DeleteRowButton_Click(Object sender, EventArgs e)
  22. {
  23.  
  24. //Delete file from the server
  25. String fileName = GridView1.SelectedRow.Cells[1].Text;
  26.  
  27. String filePath = "/UploadedUserFiles/" + User.Identity.Name + "/" + fileName;
  28.  
  29. System.IO.File.Delete(Server.MapPath(filePath));
  30.  
  31. lblDeleteResult.Text = fileName + " was successfully deleted!";
  32.  
  33. // Programmatically delete the selected record from the gridview
  34. GridView1.DeleteRow(GridView1.SelectedIndex);
  35. //Refresh Gridview
  36. GridView1.DataBind();
  37.  
  38. UploadDetails.Visible = false;
  39.  
  40. }

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
HarshKuntail is offline Offline
1 posts
since Apr 2009
Apr 20th, 2009
0

Re: Asp.net javascript confirmation box not cancelling

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?
Reputation Points: 10
Solved Threads: 0
Light Poster
julseypart is offline Offline
42 posts
since Oct 2008
Apr 22nd, 2009
0

Re: Asp.net javascript confirmation box not cancelling

READ FUNCTION BEHAVIOUR.

MARK THE THREAD AS SOLVED. If your problem resolved.
Reputation Points: 26
Solved Threads: 44
Posting Whiz in Training
mail2saion is offline Offline
247 posts
since Apr 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: ASP Site crashes after sitting idle for 30 minutes.
Next Thread in ASP.NET Forum Timeline: how should i do it?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC