Click here to Skip to main content
15,881,559 members
Articles / Web Development / ASP.NET
Tip/Trick

A Windows Form like MessageBox for ASP.NET Website

Rate me:
Please Sign up or sign in to vote.
2.11/5 (10 votes)
19 Apr 2012CPOL2 min read 68.1K   1.5K   7   8
A simple Windows Form like message box for ASP.NET applications

Introduction

This small tip discusses about a very small class that provides a Window's form like MessageBox functionality in an ASP.NET application.

Background

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 MessageBox is very simple. It contains a single function Show(). This function is a static function so that we can call this in the same way we do on a Windows form, i.e., MessageBox.Show("Some Message");

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 MessageBox.

C#
public class MessageBox
{
    // Lets keep the message coming from all the pages here
    static Dictionary<Page, Queue> pageTable = null;    

    static MessageBox()
    {
        // Create the store
        pageTable = new Dictionary<Page, Queue>();
    }

    public static void Show(string str)
    {
        // Lets find out what page is sending the request
        Page page = HttpContext.Current.Handler as Page;

        // If a valid page is found
        if (page != null)
        {
            // Check if this page is requesting message show for the first time
            if (pageTable.ContainsKey(page) == false)
            {
                // Lets create a message queue dedicated for this page.
                pageTable.Add(page, new Queue());
            }

            // Let us add messages of this to the queue now
            pageTable[page].Enqueue(str);

            // Now let put a hook on the page unload where we will show our message
            page.Unload += new EventHandler(page_Unload);
        }        
    }

    static void page_Unload(object sender, EventArgs e)
    {
        // Lets find out which page is getting unloaded
        Page page = sender as Page;

        // If a valid page is found
        if (page != null)
        {
            // Extract the messages for this page and push them to client side
            HttpContext.Current.Response.Write
            	("<script>alert('" + pageTable[page].Dequeue() + "');</script>");
        }
    }
}

And with this class in place, we can simply call the function as:

C#
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.

History

  • 20th April, 2012: First version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India

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[^]

  • Microsoft MVP 2015

Comments and Discussions

 
QuestionError in showing when use this in vs 2012 Pin
Er Atul Sharma1-Apr-15 19:12
Er Atul Sharma1-Apr-15 19:12 
QuestionVB.NET version of this not compiling Pin
Sergios S29-Apr-13 3:08
Sergios S29-Apr-13 3:08 
GeneralMy vote of 1 Pin
FerretallicA25-Apr-12 17:14
FerretallicA25-Apr-12 17:14 
QuestionWhy so much of pain Pin
perilbrain20-Apr-12 9:57
perilbrain20-Apr-12 9:57 
GeneralMy vote of 1 Pin
Selvin20-Apr-12 3:42
Selvin20-Apr-12 3:42 
GeneralRe: My vote of 1 Pin
Rahul Rajat Singh20-Apr-12 4:06
professionalRahul Rajat Singh20-Apr-12 4:06 
GeneralRe: My vote of 1 Pin
guacimapr30-May-12 3:27
guacimapr30-May-12 3:27 
GeneralMy vote of 1 Pin
Seishin#20-Apr-12 0:40
Seishin#20-Apr-12 0:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.