3

I am trying to display some text using MessageBox.Show as shown below in a page_load event in ASP.NET. Before anyone jumps on the case why I am using it in ASP.NET, the use is for debugging only on my own dev box for a special need. There's a reference to System.Windows.Forms in the app.

I used it a few years ago so I know WinForm's MessageBox works. I am using .NET 4.0 and VS 2010. I don't think anything related to this function has changed.

MessageBox.Show("Message", "Caption", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); //used also ServiceNotification option

Any ideas why the message box doesn't display? I have only that line in the code.

ADDITION:

I am VERY AWARE of the message box thing implications. It's a temporary thing for debugging only. The line won't go into production. I have no access to javascript. Please put your thought into why it doesn't work instead of why I shouldn't be using it. I have used it before in 2.0 and it does work. I want to know if the newer .NET changed anything or I misused the option.

Direct Answer: it works in Visual Studio's web server , not in IIS.

Tony_Henrich
  • 42,411
  • 75
  • 239
  • 374
  • 2
    You don't really want to show a MessageBox on the server, do you? ;-) This works only in Winforms. Have a look at [this so-question](http://stackoverflow.com/questions/4301770/how-to-show-messagebox-on-asp-net). – Tim Schmelter Aug 01 '11 at 20:16
  • Yes I do if the client and server is the same machine. I know it's a WinForm control but it can be displayed in ASP.NET and services if you supply the proper messageboxoptions – Tony_Henrich Aug 01 '11 at 20:27
  • Why not show a modal popup in the ASP.NET web page? You can limit it to authenticated users. – Chase Florell Aug 01 '11 at 20:30
  • Is this on IIS6, IIS7, IIS 7 Express or the Visual Studio Cassini server? – Kev Aug 01 '11 at 20:40
  • 1
    @Tony: If it's "a temporary thing for debugging only" does that mean that you are simply trying to provide a mechanism for displaying debugging information, or do you actually need interactive capabilities? If you only want to provide diagnostic information, then why not use a logging library or even log to the Windows Event Log? If you want to interact with the code, why not simply use the debugger? – Daniel Pryden Aug 01 '11 at 20:42
  • What account is the application pool running under that the site is running in? – Kev Aug 01 '11 at 20:46
  • @Kev your question about which web server along with the Felic's process explanation hinted me to switch from IIS to VS's web server. Thanks. – Tony_Henrich Aug 01 '11 at 21:04

3 Answers3

5

The web application is hosted in a process that does not have a desktop, so you cant see any messageboxes.

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
  • The client and server is the same box. It did work before. That's why there's MessageBoxOptions for non desktop apps. – Tony_Henrich Aug 01 '11 at 20:25
  • @Tony: It has nothing to do with the client and server running on the same physical machine. The server is running in a different operating system **process**. On Windows, service processes by default are denied the ability to interact with the desktop. It is possible to override that default, but [Microsoft does not recommend it](http://support.microsoft.com/kb/327618). ASP.NET and/or other parts of the .NET Framework may even make this more difficult to achieve than it would be with a native application. – Daniel Pryden Aug 01 '11 at 20:39
  • they may be on the same box, but `Network Service` doesn't have access to the desktop. That is usually the account that powers your ASP.NET website. So even if you launch a MessageBox, you won't see it on the desktop. – Chase Florell Aug 01 '11 at 20:40
  • ok it works in VS's web server. I knew it worked before and I wasn't losing my mind. I would like to thank Kev because along with his question the hints directed to me to use the internal server. – Tony_Henrich Aug 01 '11 at 21:03
  • "does not have a desktop" is incorrect. Even with Windows session isolation, in session 0 where IIS and the web app reside there is a desktop and the message boxes are displayed there. However, that desktop is not visible to the logon user, who can only see his/her own desktop in the user session. – Lex Li Aug 22 '22 at 15:21
1

@Tony, if you add System.Winform.dll to your rference, then you will be able to call message box.show at you development machine. But when you deploy it to some live server it will not work. So alternatively, you need to use javascript alerts. For this you can use this

 private void ShowMessage(string message)     
   {
      ScriptManager.RegisterClientScriptBlock(control, GetType(),"key","string.format("alert('{0}');",message),true);
   }
-1

Back in .Net 2.0 days, you could do this by including system.windows (I think) in your using statement.

Then, in the method, it would be system.windows.forms.Messagebox.show("foo");

I won't presume to tell you "yer doin it wrong"...

Just be aware that this will only show up on the server box, and could cause issues in a production environment.

The alert() is better, and console.log() is even better.

Joe
  • 669
  • 1
  • 8
  • 21
  • There's no javascript. I need to use this in a third party tool script. Are you saying in was available but not in 4.0 anymore? – Tony_Henrich Aug 01 '11 at 20:29
  • Don't know. Give it a try... I haven't actually tried this in a few years. – Joe Aug 01 '11 at 20:33
  • It has nothing to do with ".Net 2.0" as you can always do this when the web app is hosted on ASP.NET Development Server or IIS Express, which are web servers running in the logon user session. But the message box won't appear if hosting on IIS as session 0 is invisible to logon user. 101 of Windows session isolation. – Lex Li Aug 22 '22 at 15:20