Asp.net javascript confirmation box not cancelling

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 41
Reputation: julseypart is an unknown quantity at this point 
Solved Threads: 0
julseypart julseypart is offline Offline
Light Poster

Asp.net javascript confirmation box not cancelling

 
0
  #1
Apr 20th, 2009
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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 220
Reputation: mail2saion is an unknown quantity at this point 
Solved Threads: 33
mail2saion's Avatar
mail2saion mail2saion is offline Offline
Posting Whiz in Training

Re: Asp.net javascript confirmation box not cancelling

 
0
  #2
Apr 20th, 2009
MODIFY THE BLOW LINE OF CODE:
  1. OnClientClick="return confirm_delete()"
MARK AS SOLVED if its help you.

REGARDS
MCTS - Shawpnendu bikash maloroy
http://shawpnendu.blogspot.com
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 18
Reputation: mahendrabilla is an unknown quantity at this point 
Solved Threads: 1
mahendrabilla mahendrabilla is offline Offline
Newbie Poster

Re: Asp.net javascript confirmation box not cancelling

 
0
  #3
Apr 20th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 18
Reputation: mahendrabilla is an unknown quantity at this point 
Solved Threads: 1
mahendrabilla mahendrabilla is offline Offline
Newbie Poster

Re: Asp.net javascript confirmation box not cancelling

 
0
  #4
Apr 20th, 2009
As said by Saion, Thats also one option
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 1
Reputation: HarshKuntail is an unknown quantity at this point 
Solved Threads: 0
HarshKuntail HarshKuntail is offline Offline
Newbie Poster

Re: Asp.net javascript confirmation box not cancelling

 
0
  #5
Apr 20th, 2009
Please define Javascript onClientClick function as below
OnClientClick="return confirm_delete()"
And that should resolve your issue.

Originally Posted by julseypart View 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:

  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 41
Reputation: julseypart is an unknown quantity at this point 
Solved Threads: 0
julseypart julseypart is offline Offline
Light Poster

Re: Asp.net javascript confirmation box not cancelling

 
0
  #6
Apr 20th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 220
Reputation: mail2saion is an unknown quantity at this point 
Solved Threads: 33
mail2saion's Avatar
mail2saion mail2saion is offline Offline
Posting Whiz in Training

Re: Asp.net javascript confirmation box not cancelling

 
0
  #7
Apr 22nd, 2009
READ FUNCTION BEHAVIOUR.

MARK THE THREAD AS SOLVED. If your problem resolved.
MARK AS SOLVED if its help you.

REGARDS
MCTS - Shawpnendu bikash maloroy
http://shawpnendu.blogspot.com
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC