14

I have a form where a user can delete a record, and I want a pop-up message where the user has to click okay to confirm the delete.

Delete button:

<asp:Button ID="btnDelete" runat="server" Text="Delete" UseSubmitBehavior="false" OnClick="btnDelete_Click" OnClientClick="confirmation();" />

Confirmation function:

function confirmation() {
        var answer = confirm("Are you sure you want to delete? This action cannot be undone.")
    }

So right now, clicking the delete button executes the btnDelete_Click Sub in the code behind regardless of whether you click okay or cancel in the pop-up box. I know I can add if (answer) { -- some code here -- } in my javascript function, but is it possible to use javascript to execute code from the codebehind? Or is there another way to do this?

Jaymin
  • 2,793
  • 3
  • 18
  • 33
Sara
  • 1,483
  • 5
  • 19
  • 27
  • 2
    Just need to return false when user clicks cancel and true when clicks ok. Check out the answer by: Thit Lwin Oo – Varun Goel Mar 23 '12 at 03:56

12 Answers12

37

Please try as follows. You have to return the result of the confirmation function (true or false).

<asp:Button 
    ID="btnDelete" 
    runat="server" 
    Text="Delete" 
    UseSubmitBehavior="false" 
    OnClick="btnDelete_Click" 
    OnClientClick="return confirmation();" />

 

function confirmation() {
    return confirm("Are you sure you want to delete?");
}
T.S
  • 326
  • 3
  • 18
Thit Lwin Oo
  • 3,274
  • 3
  • 18
  • 22
  • 17
    You could just `return confirm( ... )` – Esailija Mar 23 '12 at 03:59
  • 1
    @Sara it should work. I have experienced this issue and the point is OnClientClick = "return JSFunction();" and that JSFunction return true or false. If false, it will not post back. Please try it again. – Thit Lwin Oo Mar 23 '12 at 06:49
  • This will work fine if you add {return answer;} to the end of the confirmation function without the curlies. – Joe Johnston May 19 '20 at 14:37
11

Put this in your aspx code:

OnClientClick="return confirm('Are you sure you want to delete this project?');" 
Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
Jim
  • 131
  • 1
  • 2
6

I know this is old post, but you can put the above answers into one line like this. And you don't even need to write the function.

 <asp:Button runat="server" ID="btnDelete" Text="Delete" OnClick="btnDelete_Click" OnClientClick="if ( !confirm('Are you sure you want to delete ? ')) return false;"  />
Praveen Mitta
  • 1,258
  • 2
  • 24
  • 45
  • 1
    Simple yet elegant, this worked for me as well. And less code than some of the other solutions. – T.S Jul 13 '17 at 14:52
4

please use this sample:

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return ConfirmOnDelete();"/>

<script type="text/javascript">    
   function ConfirmOnDelete() {
    if (confirm("Do you really want to delete?") == true)
        return true;
    else
        return false;
   }
</script>
Ashwini Verma
  • 7,288
  • 4
  • 33
  • 56
3

We can easily do this by using ConfirmButtonExtender,

<ajaxToolkit:ConfirmButtonExtender ID="cbeDelete" TargetControlID="btnDelete" ConfirmText="Are you sure you want to delete? This action cannot be undone." runat="server">

Its very simple...

thevan
  • 9,298
  • 52
  • 128
  • 193
1

OP, I took this a little farther. I wanted to avoid having the confirm code repeated throughout the app. I hope this might help those that wanted to separate concerns and standardize messaging.

<!- asp.net html side -->
<asp:Button 
    ID="btnRemoveOU" 
    runat="server" 
    Text="Delete OU" 
    OnClick="btnRemoveOU_Click" 
    OnClientClick="return confirmation('Delete the OU');" />

// javascript Side 
function confirmation(action) {
    var answer = confirm("Are you sure you want to " + action + "? This action cannot be undone.")
    return answer;
}

hth

Joe Johnston
  • 2,218
  • 1
  • 25
  • 48
0

I think above should work if you are not able to use it with Button, Try the <asp:link button>. Mine works just fine.

ASPX page:-

<asp:LinkButton  ID="takeActionBtn" CausesValidation="false" runat="server" 
        onclientclick="return confirm('Do you really want to perform Action ?');"> Take Action </asp:LinkButton>

VB server codebehind:-

Protected Sub takeActionBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles takeActionBtn.Click

End Sub

One thing I noted is that if you are using control (ascx) within a page you may need to to off/on at page or control level AutoEventWireup="false" <%@ control in <%@ page

Good Luck !!

Joe Mike
  • 1,090
  • 9
  • 16
PatBad
  • 11
  • 2
0

1) Form Design

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type = "text/javascript">
    function Confirm() {
        var confirm_value = document.createElement("INPUT");
        confirm_value.type = "hidden";
        confirm_value.name = "confirm_value";
        if (confirm("Do you want to save data?")) {
            confirm_value.value = "Yes";
        } else {
            confirm_value.value = "No";
        }
        document.forms[0].appendChild(confirm_value);
    }
</script>
</head>
<body>
<form id="form1" runat="server">
  <asp:Button ID="btnConfirm" runat="server" 
 OnClick = "OnConfirm" Text ="Raise Confirm" OnClientClick = "Confirm()"/>
</form>
</body>
</html>

2) Codebehind

public void OnConfirm(object sender, EventArgs e)
{
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == "Yes")
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(),"alert('You    clicked YES!')", true);
}
else
{
    this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!')", true);
}
}

3) When you click the delete button confirmation box will be displayed.

4) If you click ok then OK part will work in code else No Part

5) The same method in GRIDVIEW DELETE BUTTON.

0

You can just put:

onclientclick = "return (window.confirm('text message'));

in the ASPX page, it does the same thing.

Gustavo Morales
  • 2,470
  • 9
  • 26
  • 32
Garry_G
  • 149
  • 4
  • 12
0

If you want to excute some code in server side without actually making the post back, you have to use ajax to do that.

function confirmation() {
        var answer = confirm("Are you sure you want to delete? This action cannot be undone.")
        if(answer)
        {
           $.post("ajaxserverpage.aspx?item=34",function(data){
              alert(data); 
          });
        } 
        return false; 
    }

and have a page called ajaxserverpage.aspx and in the page load of that page, check the query string and execute your relevant server side code and return some string

   protected void Page_Load(object sender, EventArgs e)
   {
        if (Request.QueryString["item"] != null)
        {
          // read the item and do your stuff here
         Response.Write("Deleted succssfully");
         Response.End();
        }
    }

You can also use generic handler (.ashx) to do your ajax server side processing instead of the .aspx file. I would prefer this method.

Shyju
  • 197,032
  • 96
  • 389
  • 477
0

Yet another way to achieve this would be using the AJAX Toolkit ConfirmButton Extender, as shown here:

http://www.ezineasp.net/Samples/ASP-Net-AJAX-cs/Control-Toolkit/AJAX-ConfirmButton-Control/Default.aspx

mellodev
  • 1,527
  • 12
  • 20
-2

If you want javascript to access methods in the code behind you can always expose them in a RESTful service.

$.ajax({
  url: 'url to the rest call',
  success: successfuntion(),
  dataType: 'text/json'
});

Have the REST call do whatever logic you need and send some data back to indicate a success. Then just use the successfunction to remove the element from the DOM.

Brian Warfield
  • 347
  • 1
  • 11