deleting record when it shouldn't

2019-09-21 15:23发布

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>

3条回答
Emotional °昔
2楼-- · 2019-09-21 15:38

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()";
查看更多
欢心
3楼-- · 2019-09-21 15:46

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.

查看更多
forever°为你锁心
4楼-- · 2019-09-21 15:55

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.

查看更多
登录 后发表回答