使用Ajax和确认消息进行删除

时间:2015-08-31 16:04:02

标签: javascript jquery asp.net

我想点击删除ImageButton时收到确认消息。如果用户选择“确定”则执行删除操作,否则如果单击“取消”则不会执行任何操作。

<asp:ImageButton ID="ImageButton1" runat="server"  ImageUrl="~/images/forDesign/delete.png"   OnClientClick="ConfirmDelete()"  />

<script type="text/JavaScript" 
src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
    function ConfirmDelete() {
        var x = confirm("Are you sure you want to delete?");
        if (x)
            $.ajax({
                type: "get",
                url: "SearchTicket.aspx/Delete",
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (response) {
                    alert(response.status);
                },
                error: function () {
                    alert("error");
                }
            });           
    }

在代码背后我有:

 [WebMethod]
public void Delete()
{
    string code = DetailsViewCodeTicket.Rows[2].Cells[1].Text.ToString();
    Ticket.TicketCusDelete(code);
    DetailsViewCodeTicket.DataBind();
}

但是当我点击ImageButton时出现此错误:

enter image description here

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您应首先点击启动灯箱....然后,当用户确认您触发了ajax呼叫时......

这是一个例子:http://jsfiddle.net/leojavier/976oxwmk/

<button>remove</button>

JS

$('button').on('click', function(){

    var confirmation = confirm("are you sure you want to remove the item?");

    if (confirmation) {
     // execute ajax
        alert('removed');
    }
})