Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Testing with a Client-Side Web MessageBox


Client-Side Web MessageBox

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 you’re developing the application on the server machine, or if you’re testing the application locally. It just can’t 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 you’ve done this, you could place a call to the MessageBox.Show method. However, note that you’re not allowed to show a modal dialog when the application—the web application in this case—is 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 won’t 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)

It’s 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. Let’s 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 you’re 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 it’s 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].


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.