ASP.NET confirm before executing codebehind

2019-01-19 11:30发布

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?

11条回答
三岁会撩人
2楼-- · 2019-01-19 12:23

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.

查看更多
乱世女痞
3楼-- · 2019-01-19 12:24

You can just put:

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

in the ASPX page, it does the same thing.

查看更多
不美不萌又怎样
4楼-- · 2019-01-19 12:27

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?");
}
查看更多
\"骚年 ilove
5楼-- · 2019-01-19 12:28

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.

查看更多
何必那么认真
6楼-- · 2019-01-19 12:28

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.

查看更多
登录 后发表回答