1

I wants to display an OK/CANCEL message box. I have written like this.

   if (MessageBox.Show("Are you sure you wants to Save the details ?  ", "Validate", MessageBoxButtons.OKCancel) == DialogResult.OK)
    {
         // do something if "OK "
    }

it works locally well.. but on IIS shows an error " Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application." please help.

ARATHY
  • 349
  • 5
  • 13
  • 29
  • 4
    `MessageBox` is a Windows GUI widget, not a browser widget. If you call it from the server, it's shown **on the server**, not on the visitor's browser. If you want to show a message box on the client side, you need to use HTML and Javascript. – zneak Sep 03 '13 at 03:52
  • 2
    Since you are developing a web application it is not recommended that you show a modal dialog. Instead you should be showing an confirm dialog using javascript. – Vishweshwar Kapse Sep 03 '13 at 03:53
  • ohh thankyou :) how is it do with javascript ? @ zneak @ Vishweshwar Kapse – ARATHY Sep 03 '13 at 03:56

1 Answers1

3

To get the effect I believe you are wanting, you'll want to use the Javascript confirm() function. It is typically used like this:

<asp:Button runat="server" OnClientClick="return confirm('Are you sure you want to save the details?')" id="btnSubmit" OnClick="btnSubmit_Click" />

This would, upon clicking the button, display a confirmation box that would stop the server OnClick event from firing if the user clicks No, Cancel, etc.

OnClientClick will render as an onclick event on the <input> tag. The rest is determined by the browser's handling of Javascript. See What's the effect of adding 'return false' to a click event listener? for more details on what the return value of the code in OnClick (OnClientClick) does.

Here's an example of the return values of confirm() using Chrome. For the first execution I had clicked Cancel.

JS confirm() example in Chrome)

Community
  • 1
  • 1
jdphenix
  • 15,022
  • 3
  • 41
  • 74
  • Don't forget to close quotes and brackets ;) – afzalulh Sep 03 '13 at 03:56
  • For completeness, you might want to include OnClick handler for the button. That's missing currently!! – Prash Sep 03 '13 at 03:56
  • how will be the confirm() ?? @jdphenix – ARATHY Sep 03 '13 at 04:08
  • @ARATHY `OnClientClick` will render as an `onclick` event on the `` tag. The rest is determined by the browser's handling of Javascript. See http://stackoverflow.com/questions/128923/whats-the-effect-of-adding-return-false-to-an-onclick-event for more details on what the return value of the code in `OnClick` (`OnClientClick`) does. – jdphenix Sep 03 '13 at 04:18
  • function confirm() { if (confirm("Are you sure you want to save the details??") == true) return true; else return false; } is it means when return true it will goes to onclick event automatically ?? @jdphenix – ARATHY Sep 03 '13 at 04:26
  • That is a quite verbose way of expressing it - it can be simply `return confirm('Are you sure you want to save the details?')`. The `return` works very similarly to `return` in C#, and the Javascript in `onclick` returning `false` to most browsers will keep the default behavior from occuring (i.e. NOT posting back). – jdphenix Sep 03 '13 at 04:32
  • Got the cut and clear solution from http://www.aspsnippets.com/Articles/ASPNet-Server-Side-Yes-No-Confirmation-Box-using-JavaScript.aspx – ARATHY Sep 03 '13 at 04:45
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/36694/discussion-between-jdphenix-and-arathy) – jdphenix Sep 03 '13 at 04:52