Message Box In ASP.NET‏


WebMsgBox class is a message box for ASP.NET. 

Recently, I needed a Windows MessageBox like class for ASP.NET. Doing some search on Google, I found some code for message box in VB.NET and converted code from VB.NET to C# and made some changes. Unfortunately, I forgot the URL where I found the code. 

WebMsgBox Class

WebMsgBox class represents a message box for ASP.NET applications. This class has a static method Show, which is used to display a message box. The Show method takes a single argument of string type, which is the message you want to display.

using System;
using Microsoft.VisualBasic;
using System.Text;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestWebMsgApp
{
      public class WebMsgBox
      {
            protected static Hashtable handlerPages = new Hashtable();            
            private WebMsgBox()
            {
            }

            public static void Show(string Message)
            {
                  if (!(handlerPages.Contains(HttpContext.Current.Handler)))
                  {
                        Page currentPage = (Page)HttpContext.Current.Handler;
                        if (!((currentPage == null)))
                        {
                              Queue messageQueue = new Queue();
                              messageQueue.Enqueue(Message);
                              handlerPages.Add(HttpContext.Current.Handler, messageQueue);
                              currentPage.Unload += new EventHandler(CurrentPageUnload);
                        }
                  }
                  else
                  {
                        Queue queue = ((Queue)(handlerPages[HttpContext.Current.Handler]));
                        queue.Enqueue(Message);
                  }
            }

            private static void CurrentPageUnload(object sender, EventArgs e)
            {
                  Queue queue = ((Queue)(handlerPages[HttpContext.Current.Handler]));
                  if (queue != null)
                  {
                        StringBuilder builder = new StringBuilder();
                        int iMsgCount = queue.Count;
                        builder.Append("");
                        handlerPages.Remove(HttpContext.Current.Handler);
                        HttpContext.Current.Response.Write(builder.ToString());
                  }
            }
      }

} 

How to use WebMsgBox?

If you want to display a message box on a button click, add the following code to the button click event handler.

WebMsgBox.Show("Your message here");
Related Posts Plugin for WordPress, Blogger...