-->

ASP.NET confirm before executing codebehind

2019-01-19 12:19发布

问题:

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?

回答1:

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?");
}


回答2:

Put this in your aspx code:

OnClientClick="return confirm('Are you sure you want to delete this project?');" 


回答3:

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>


回答4:

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...



回答5:

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;"  />


回答6:

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.



回答7:

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



回答8:

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.



回答9:

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 !!



回答10:

You can just put:

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

in the ASPX page, it does the same thing.



回答11:

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.