This small tip discusses about a very small class that provides a Window's form like
MessageBox functionality in an ASP.NET application.
I come from a C++/MFC and C#/Winforms background. The developers working with these will definitely agree to the fact that
MessageBox is a very useful UI element that can be used to display quick information to the users and also proves very useful in quick debugging. I started working in ASP.NET last year and missed this small fellow a lot. So I decided to have one for me which will use server side code to provide a quick message pop up on the web page using client side scripting.
Using the Code
The class for
is very simple. It contains a single function
. This function is a
function so that we can call this in the same way we do on a Windows form, i.e.,
The main thing to notice in this class is that it keeps track of the messages coming from all the pages of the website. Also, it shows these messages when the
Page_Unload is getting called after the
postback. The reason for doing that is that the user should be able to see the web page in the background when the message comes on screen (which will not be the case if we don't put it in
Unload event). Here is the class for
public class MessageBox
static Dictionary<Page, Queue> pageTable = null;
pageTable = new Dictionary<Page, Queue>();
public static void Show(string str)
Page page = HttpContext.Current.Handler as Page;
if (page != null)
if (pageTable.ContainsKey(page) == false)
pageTable.Add(page, new Queue());
page.Unload += new EventHandler(page_Unload);
static void page_Unload(object sender, EventArgs e)
Page page = sender as Page;
if (page != null)
("<script>alert('" + pageTable[page].Dequeue() + "');</script>");
And with this class in place, we can simply call the function as:
MessageBox.Show("Hello World 1");
Which would result in a message box on screen like:
Points of Interest
After completing this small exercise, I am thinking of extending this class to support all the overloaded versions of the
MessageBox.Show() function. Once I do that, I will perhaps update this small tip.
20th April, 2012: First version
I Started my Programming career with C++. Later got a chance to develop Windows Form applications using C#. Currently using C#, ASP.NET & ASP.NET MVC to create Information Systems, e-commerce/e-governance Portals and Data driven websites.
My interests involves Programming, Website development and Learning/Teaching subjects related to Computer Science/Information Systems. IMO, C# is the best programming language and I love working with C# and other Microsoft Technologies.
- Microsoft Certified Technology Specialist (MCTS): Web Applications Development with Microsoft .NET Framework 4
- Microsoft Certified Technology Specialist (MCTS): Accessing Data with Microsoft .NET Framework 4
- Microsoft Certified Technology Specialist (MCTS): Windows Communication Foundation Development with Microsoft .NET Framework 4
If you like my articles, please visit my website for more: www.rahulrajatsingh.com[^]