ASP.NET confirm before executing codebehind

孤街醉人 提交于 2019-11-28 11:59:18
Thit Lwin Oo

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

Put this in your aspx code:

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

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>

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

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

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.

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

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.

PatBad

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

Garry_G

You can just put:

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

in the ASPX page, it does the same thing.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!