When I began the programming for [snipped link], I quickly noticed that Asp.net does not support the MessageBox class that I was familiar with in windows forms.

Since Asp.net runs on the server it doesn't have access to produce a modal dialog box on the client. However, by using a scripting language that runs in the clients browser a similar construct is available. In javascript the window.alert() function will produce a modal dialog similar to what you would expect from MessageBox in windows forms.

So, I decided to create a static class MessageBox with a static method Show(), which will give you the ablility to to still use the syntax MessageBox.Show("Your Message"); and then convert it into the javascript window.alert("Your Message"); and add the script to the end of the current response stream.

I've written a how to on my blog at: http://sceptermt.blogspot.com/2008/02/how-to-create-message-box-class-in-c.html that shows the source code that I used in C# for my web application and details on how it works.

You could also write overloads for the static show() method to allow the class to do a javascript confirm() or a prompt(). I haven't needed that functionality yet so I've just kept it simple.

Recommended Answers

You can add reference (System.Windows.dll) to your web-based application and use MessageBox!

Jump to Post

All 4 Replies

You can add reference (System.Windows.dll) to your web-based application and use MessageBox!

You can add reference (System.Windows.dll) to your web-based application and use MessageBox!

Yes, importing the windows forms version of MessageBox does work in web forms. However, the code is being executed on the server and thus the MessageBox will only be displayed on the servers computer. It would never reach the client side. That's why you have to use a client side scripting language like javascript to get the clients browser to render the modal dialog.

Try this,


catch (WebException ex)
    ScriptManager.RegisterStartupScript(this, this.GetType(), "ALERT", "alert('Erro exception" + ex.Message + "');", true);

Easy drag and drop form builder.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.