ARTICLE

ASP.NET MessageBox

Posted by Mahesh Chand Articles | How do I September 11, 2006
The attached source code contains a class WebMsgBox, which is a message box written in C# for ASP.NET applications.
Reader Level:
Download Files:
 

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("<script language='javascript'>");

                        string sMsg;

                        while ((iMsgCount > 0))

                        {

                              iMsgCount = iMsgCount - 1;

                              sMsg = System.Convert.ToString(queue.Dequeue());

                              sMsg = sMsg.Replace("\"", "'");

                              builder.Append("alert( \"" + sMsg + "\" );");

                        }

                        builder.Append("</script>");

                        handlerPages.Remove(HttpContext.Current.Handler);

                        HttpContext.Current.Response.Write(builder.ToString());

                  }

            }

      }

 

} 

 

How to use WebMsgBox?

 

Download the attached WebMsgBox.cs and add it to your project. You will have to change the namespace of the class. Change the following line to your project's namespace in WebMsgBox.cs file:

 

namespace TestWebMsgApp

 

Now call WebMsgBox.Show method when you need to display a message box. For example, 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");

Login to add your contents and source code to this article
Article Extensions
Contents added by santosh raul on May 31, 2012

I have downloaded the source & using it but  I am getting Jscript runtime error.
Please throw some light on it.

Regards
Santosh Raul.

post comment
     

its rly grt...thnks

Posted by Foysal Alan Jan 08, 2013

Thanks for that articles buddy

Posted by jitendra prajapat Jan 04, 2013

THANKS A LOT, it rocks in ASP.NET web apps.

Posted by Narendran N Oct 30, 2012

NICE ONE... PLUS ONE FOR THE GOOD GUY!!

Posted by Ronald de Fiesta Oct 27, 2012

Tanks Chand, web message class was quite useful. It does solved my challenge.

Posted by fiyin ok Aug 16, 2012
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter