How To: Create a MessageBox Class for Web Forms in C#

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2008
Posts: 2
Reputation: ScepterMT is an unknown quantity at this point 
Solved Threads: 0
ScepterMT ScepterMT is offline Offline
Newbie Poster

How To: Create a MessageBox Class for Web Forms in C#

 
0
  #1
Mar 21st, 2008
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.
Last edited by Narue; Mar 22nd, 2008 at 9:13 am. Reason: Snipped self-promotional link
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: How To: Create a MessageBox Class for Web Forms in C#

 
0
  #2
Mar 23rd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 2
Reputation: ScepterMT is an unknown quantity at this point 
Solved Threads: 0
ScepterMT ScepterMT is offline Offline
Newbie Poster

Re: How To: Create a MessageBox Class for Web Forms in C#

 
0
  #3
Mar 23rd, 2008
Originally Posted by RamyMahrous View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C# Forum


Views: 4970 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC