| | |
How To: Create a MessageBox Class for Web Forms in C#
Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2008
Posts: 2
Reputation:
Solved Threads: 0
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/0...lass-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.
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/0...lass-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.
Last edited by Narue; Mar 22nd, 2008 at 9:13 am. Reason: Snipped self-promotional link
Check This Out: http://www.sceptermarketingtechnologies.com
You can add reference (System.Windows.dll) to your web-based application and use MessageBox!
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
•
•
Join Date: Mar 2008
Posts: 2
Reputation:
Solved Threads: 0
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.
Check This Out: http://www.sceptermarketingtechnologies.com
![]() |
Other Threads in the C# Forum
- Previous Thread: How to make the comparison
- Next Thread: Reading Text Files
Views: 4970 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap box broadcast button buttons c# chat check checkbox class client code color combobox control conversion csharp custom database datagridview dataset datetime degrees draganddrop drawing encryption enum excel file files form format forms ftp function gdi+ http image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






