It is no-brainer that Visual Studio .NET provides great support for debugging web applications. Likewise, breakpoints, quick watch and output windows, tracing, and logging unquestionably form a rich debug toolkit. So what more could you want? Many developers, including myself, sometimes prefer a simple message box to pop up and show a given message. Recommending message boxes as a debugging technique might not be new; however, it can be a quick and helpful trick to use during the development cycle. How can you display a message box within an ASP.NET application?
It goes without saying that importing the Windows Forms assembly is not a good idea. It might perhaps work if youre developing the application on the server machine, or if youre testing the application locally. It just cant work in a real-world testing environment where server and client machines are physically distant.
Technically speaking, nothing prevents you from importing the System.Windows.Forms assembly in an ASP.NET project. Once youve done this, you could place a call to the MessageBox.Show method. However, note that youre not allowed to show a modal dialog when the applicationthe web application in this caseis not running in UserInteractive mode. To force the modal dialog box, you must add one of the following styles: ServiceNotification or DefaultDesktopOnly. In both cases, the message box is displayed on the active desktop. However, ServiceNotification displays the message box even if there is no user logged on to the computer.
At any rate, the message box is displayed on the server machine. The client is blocked and wont receive the page until you click to dismiss the message box.
If this is what you want, go on and use the following code:
MessageBox.Show(msg, caption, _ MessageBoxButtons.OK, _ MessageBoxIcon.Asterisk, _ MessageBoxDefaultButton.Button1, _ MessageBoxOptions.DefaultDesktopOnly)
Its more likely, instead, that you want to display an informative message box on the client machine. In this case, you can only inject some Javascript code in the page. Lets see an example.
The following code illustrates a server-side method that injects the minimal Javascript code needed to pop up a message.
Sub MsgBox(msg As String) Response.Write("<script>alert('" + _ msg + _ "');</" + _ "script>"); End Sub
You place a call to this method whenever you need to display a message in the page. For example, if youre building a DataGrid and want to be notified when each item is created, do the following:
Sub ItemCreated(sender As Object sender, _ e As DataGridItemEventArgs) MsgBox(e.Item.ItemType.ToString()) End Sub
The source code of the page is prefixed with all the <script> tags you inserted, and all of them are displayed in an order determined by the page lifecycle. This technique works well only for informative messages that do not require interaction, but its straightforward to use and effective. Enjoy.
Dino Esposito is Wintellect's ADO.NET and XML expert, and a trainer and consultant
based in Rome, Italy. Dino is a contributing editor to Windows Developer
Network and MSDN Magazine, and the author of several books for Microsoft
Press including Building Web Solutions with ASP.NET and ADO.NET
and Applied XML Programming for .NET. Contact Dino at [email protected].