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>
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 :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 :
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
to
and make
ConfirmationBox()
return false if the action is not confirmed.As Jim said you have to have
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.