-1

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>
Community
  • 1
  • 1
Mathematics
  • 7,314
  • 25
  • 77
  • 152

3 Answers3

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.

yoozer8
  • 7,361
  • 7
  • 58
  • 93
  • thanks it fixed half of the problem, however I am not able to delete a record now, when I press "yes" – Mathematics May 01 '13 at 13:20
  • The key to that is in my last sentence "...make `ConfirmationBox()` return false *if the action is not confirmed*". If the action *is* confirmed, you need to return true. – yoozer8 May 01 '13 at 13:24
  • action is confirmed or not depends on yes or no, and yes is returning true, but nothing happening afterwards Sir – Mathematics May 01 '13 at 13:25
  • Oh, I see your updated code. It's the `ConfirmationBox()` method that needs to return true or false, not the click event of the yes or no button. – yoozer8 May 01 '13 at 13:26
1

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()";
Semih Yagcioglu
  • 4,011
  • 1
  • 26
  • 43
0

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.

Nilesh Thakkar
  • 2,877
  • 1
  • 24
  • 43
  • Sir if you've not read my complete answer, then do it first. To make it work you need to do what I said, without that Jim's answer won't solve the problem, that you should have figured out already. – Nilesh Thakkar May 01 '13 at 14:23