最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Use Ajax and Confirm message for delete - Stack Overflow

programmeradmin4浏览0评论

I want to get a confirm message on clicking delete ImageButton. If the user selects 'Ok' then delete is done, else if 'Cancel' is clicked nothing happens.

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

<script type="text/JavaScript" 
src=".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");
                }
            });           
    }

and in code behind I have:

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

but when I clicking the ImageButton I get this error:

How can I do that ?

I want to get a confirm message on clicking delete ImageButton. If the user selects 'Ok' then delete is done, else if 'Cancel' is clicked nothing happens.

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

<script type="text/JavaScript" 
src="http://ajax.googleapis./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");
                }
            });           
    }

and in code behind I have:

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

but when I clicking the ImageButton I get this error:

How can I do that ?

Share Improve this question asked Aug 31, 2015 at 16:04 Gholamreza AsadiGholamreza Asadi 1051 gold badge4 silver badges10 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You should launch your lightbox on click first.... and then, when the user confirm you trigger your ajax call...

Here an example: http://jsfiddle/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');
    }
})

I think you only need to add curly brackets:

if(x){
ajax code ....
}

so this will be the overall code:

<asp:ImageButton ID="ImageButton1" runat="server"  ImageUrl="~/images/forDesign/delete.png"   OnClientClick="ConfirmDelete()"  />
<script type="text/JavaScript" src="http://ajax.googleapis./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");
                }
            });
        }
    }
</script>
发布评论

评论列表(0)

  1. 暂无评论