Itzick Binder
Itzick Binder

Reputation: 175

MessageBox continues automatically without pressing on button

I have a messagebox with two buttons and a text. This is the relevant code:

var result = MessageBox.Show("just a text","just a title",MessageBoxButtons.OKCancel,System.Windows.Forms.MessageBoxIcon.Warning,System.Windows.Forms.MessageBoxDefaultButton.Button1, System.Windows.Forms.MessageBoxOptions.ServiceNotification | System.Windows.Forms.MessageBoxOptions.RightAlign);
if (result == DialogResult.OK)
{
    ... code ...
}

My problem is that the program enters the code inside the "if" automatically and the pop up message box doesn't even show up. Even when i debug it I see that the code goes to the "if" row and the result is DialogResult.OK.

I am using web forms and ASP.Net.

the namespace for the MessageBox is "System.Windows.Forms".

I tried clearing the cache and even iisreset.

What else could help me?

Thank you in advance.

Upvotes: 2

Views: 2452

Answers (2)

Behzad
Behzad

Reputation: 3580

There are two issues here. In the code logic, the MessageBox Show method is concurrent with the main thread. So when running the Show method immediately the codes after it runs, and the main thread does not wait for the answer! If you want to be the main thread wait for closing message, you should use the ShowDialog function of MessageBox. (MSDN Reference)

But now you're working In the ASP, and this is very different from Windows, as our dear friend CodeCaster also said, ASP the server side not the client side! So your message will be displayed on the server system, not on the user system who are watching your web site!

Upvotes: 0

CodeCaster
CodeCaster

Reputation: 151588

I am using web forms and ASP.Net.

the namespace for the MessageBox is "System.Windows.Forms".

Don't do that. An ASP.NET site runs at the server, so there's nobody who can click the MessageBox if it even would pop up on the server.

If you want to show a popup to the user, it'll have to be in Javascript. See How to make a simple yes/no popup in ASP.NET that return the result back to my c#?.

Upvotes: 7

Related Questions