deleting record when it shouldn't

空扰寡人 提交于 2019-12-13 10:29:57

问题


I am trying to show a confirmation box, which works perfectly with Confirm but doesn't work with my custom message box,

This works,

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {

        LinkButton link = (LinkButton)e.Row.Cells[4].Controls[2];
        if (link != null)
        {
            link.OnClientClick = "return confirm('Do you really want to delete?')";
        }
    }
}

BUT when i put this instead

link.OnClientClick = "ConfirmationBox()";


 function ConfirmationBox() 
    { 
    $.blockUI({ message: $('#question'), css: { width: '275px' } 
    }); 
    }

It shows message box but then it also deleting my record :'(

Still confused ? check this out,

Command field showing messagebox

Edit

<script type="text/javascript">
 $(document).ready(function() { 

 $('#yes').click(function() { 
        $.unblockUI(); 
        return true;
    });

    $('#no').click(function() { 
        $.unblockUI(); 
        return false; 
    }); 
}); 
 </script>

回答1:


As Jim said you have to have

link.OnClientClick = "return ConfirmationBox()";

ConfirmationBox should always return false. You need to have one more button which will perform the delete operation and you need to fire that button's click event if user press yes button. I hope that makes sense.




回答2:


Look at the difference between the two OnClientClick events. The one that works properly returns a value, whereas the one that does not does not.

When the button is clicked, the button action is performed. The on-click action is also performed. However, if the on-click action returns false, the button's action is cancelled. Change

link.OnClientClick = "ConfirmationBox()";

to

link.OnClientClick = "return ConfirmationBox()";

and make ConfirmationBox() return false if the action is not confirmed.




回答3:


The second option most probably does not return false, this is why your record is deleted in any case. You can verify it by changing it to :

function ConfirmationBox() 
    { 
      $.blockUI({ message: $('#question'), css: { width: '275px' }}); 
      return false;
    }

Not that this would prevent deleting the record, but also will not let you to delete it. You would need something that would return the result of the UI control.

Also you should modify the link :

link.OnClientClick = "return ConfirmationBox()";


来源:https://stackoverflow.com/questions/16318753/deleting-record-when-it-shouldnt

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