4

How to I display a confirm message box before deleting records? Buttons should be YES or NO only. Not OK or CANCEL. I have this code but it only works for c# winforms...

if (MessageBox.Show("Delete record no. " + numID.Text + "?", "Confirm User Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
    //codes to delete records
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
user2971155
  • 195
  • 2
  • 4
  • 11
  • you need to add a reference to System.Windows.Forms library and you have to refer it in your program like -> using System.Windows.Forms; Statement. – Sudhakar Tillapudi Nov 14 '13 at 03:27
  • I'm getting this error `The type or namespace name 'Windows' does not exist in the namespace 'System' (are you missing an assembly reference?)` – user2971155 Nov 14 '13 at 03:29
  • 5
    You don't want to use a Windows.Forms.MessageBox. Don't be silly. – SQLMason Nov 14 '13 at 03:30
  • Depends on the technology you are using, a good recommendation would be to use Javascript's confirm function. Please embed it into your code to let the user decide and then continue to your action. There is a [similar question about it here](http://stackoverflow.com/a/5867618/428813). [Here is some more light](http://www.w3schools.com/js/js_popup.asp). [And a good example in this link](http://www.aspsnippets.com/Articles/ASPNet-Server-Side-Yes-No-Confirmation-Box-using-JavaScript.aspx). Hope it helps, – Ramon Araujo Nov 14 '13 at 03:28
  • doesn't have yes/no options – user2971155 Nov 14 '13 at 03:31
  • The accepted answer to the following question has a jquery solution. – Joe Jan 22 '15 at 10:17

2 Answers2

1

If you are displaying this client side, then you should use Javascript. A good way to do this is to use the jQuery dialog method. For example:

Markup:

<div id="dialog-confirm">This is the content</div>

Javascript:

 $( "#dialog-confirm" ).dialog({
      resizable: false,
      height:280,
      modal: true,
      buttons: {
        "Yes": function() {
          $( this ).dialog( "close" );
        },
        "No": function() {
          $( this ).dialog( "close" );
        }
      }
 });

Fiddle: http://jsfiddle.net/ghLpV/

John Koerner
  • 37,428
  • 8
  • 84
  • 134
1
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type = "text/javascript">
        function Confirm() {
            var confirm_value = document.createElement("INPUT");
            confirm_value.type = "hidden";
            confirm_value.name = "confirm_value";
            if (confirm("Do you want to save data?")) {
                confirm_value.value = "Yes";
            } else {
                confirm_value.value = "No";
            }
            document.forms[0].appendChild(confirm_value);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
      <asp:Button ID="btnConfirm" runat="server" OnClick = "OnConfirm" Text = "Raise Confirm" OnClientClick = "Confirm()"/>
    </form>
</body>
</html>

Check this link:

http://aspsnippets.com/Articles/Server-Side-Code-Behind-Yes-No-Confirmation-Message-Box-in-ASPNet.aspx

Monika
  • 2,172
  • 15
  • 24