Blue Theme Orange Theme Green Theme Red Theme
 
ASP.NET Web Hosting – Click Here
Home | Forums | Videos | Photos | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » How do I » ASP.NET MessageBox

ASP.NET MessageBox

The attached source code contains a class WebMsgBox, which is a message box written in C# for ASP.NET applications.

Author Rank:
Technologies: .NET 1.0/1.1,Visual C# .NET
Total downloads : 2350
Total page views :  133010
Rating :
 4.59/5
This article has been rated :  17 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
WebMsgBox.zip
 
ArticleAd
Become a Sponsor




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
 [Top] Rate this article
 About the author
 
Mahesh Chand
Mahesh is a software consultant, architect, author, MCP, MVP, and founder of C# Corner. He has over 13 years of experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries including Microsoft, Unisys, Barclay’s, Centocor (J&J), McGraw-Hill, Excelon, PMI, and University of Pennsylvania Hospital. Since year 2000, he is been working with, ASP.NET, SQL Server, C# and .NET. His latest experience and interest is Silverlight, WPF, WCF, XAML and .NET 3.5. If you need any consulting in ASP.NET, AJAX, WPF, WCF, or XAML, contact him at mahesh AT c-sharpcorner DOT com
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDFâ„¢ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Boost the performance of your .NET applications
“ANTS Profiler took us straight to the specific areas of our code which were the cause of our performance issues." Terry Phillips, Sr. Developer, Harley-Davidson Dealer Systems. Download your free trial of ANTS Profiler.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
WebMsgBox.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
ArticleAd
Become a Sponsor
Latest Comments:
Subject Posted By Posted On
Please urgent help vasanth6/13/2007
how to implement Yes/No Button in messagebox using Asp.Net.
Reply | Email | Delete | Modify | 
HiyaSana6/15/2007
Suppub article............Many thx
Reply | Email | Delete | Modify | 
WebMsgBox is not working with AJAXJoyal7/5/2007
WebMsgBox is not working with AJAX Button. What should be the option for that?
Reply | Email | Delete | Modify | 
WebMsgBox is not working with AJAXJoyal7/5/2007
WebMsgBox is not working with AJAX Button. What should be the option for that?
Reply | Email | Delete | Modify | 
WebMsgBox is not working with AJAXJoyal7/5/2007
WebMsgBox is not working with AJAX Button. What should be the option for that?
Reply | Email | Delete | Modify | 
 
 
Re: WebMsgBox is not working with AJAXMahesh7/11/2007

Joyal,
I've not used WebMsgBox with AJAX and do not know the answer for that. If I do find, will post it here.

Reply | Email | Delete | Modify | 
Not workingJuien7/11/2007
WebMsgBox is not working with WebForm Button.
Reply | Email | Delete | Modify | 
 
 
Re: Not workingMahesh7/11/2007
I am not sure what do you mean by it is not working with WebForm button. Did you mean if you call it on button click event handler? I've used it on a button click event handler and seems to be working just fine. Do you have pop-up blocker installed on your machine?
Reply | Email | Delete | Modify | 
webmsgboxtonygreaves17/15/2007
After uploading my web app to production the webmsgbox produces a permissions errors from user's desktop. How can I correct this?
Reply | Email | Delete | Modify | 
Works Great!Luis11/14/2007
Thanks for the class, worked great for me. Thanks.
Reply | Email | Delete | Modify | 
it works!!anagha12/11/2007
Hi, Thanx a lot. gr8........
Reply | Email | Delete | Modify | 
How to useChris12/30/2007
Thanks for the code, this is esactly what ive been looking for. Ive added the class and changed the namespace to the name of my application but when i compile i get: Error 1 Name 'webmsgbox' is not declared. when i add to the click event of my code. Sorry to bother you, if you can help i would be much appreciated Chris Anderson
Reply | Email | Delete | Modify | 
error near "j" mancy1/19/2008
please help when ever the Show("Your message here"); is called an error message appears error near "j" tell me what to do??? thanks
Reply | Email | Delete | Modify | 
 
 
Re: error near "j" Shakti Singh3/19/2008
Same problem I am getting
Reply | Email | Delete | Modify | 
Error MessageShakti Singh3/19/2008
I am using same class in my project but I am getting some error , there is script manager and updatepanel on my page. I think that target error is related to AJAX supporing. Can you please tell me that is your code support AJAX or not? Have you test it on AJAX Thanks Shakti Singh Dulawat
Reply | Email | Delete | Modify | 
Not Working for meJoe3/19/2008
case "Skip": ZNT.DataAccess.NOA noa = (ZNT.DataAccess.NOA)Session["objNOA"]; noa.Accepted = "RET_MLD"; noa.RetainerType = "SKIP"; ZNT.Business.NOAManager.UpdateNOA(noa); ZNT.Common.WebMsgBox .Show ("You document has been saved"); Response.Redirect("~/UI/CaseStage/StagePicker.aspx"); break;
Reply | Email | Delete | Modify | 
change the headermahkameh8/21/2008
thank for the class, is working fine for me but how can I change the header?
Reply | Email | Delete | Modify | 
change the headermahkameh8/21/2008
thank for the class, is working fine for me but how can I change the header?
Reply | Email | Delete | Modify | 
plz plz helpsriraj10/1/2008
hi mine is not a project but a website so wat namespace should i replace TestWebMsgApp with??
Reply | Email | Delete | Modify | 
 
 
Re: plz plz helpMahesh10/1/2008
Your web application namespace name.
Reply | Email | Delete | Modify | 
helpsriraj10/2/2008
but i dont have any namespace for my website. now tell me how to replace the namespace???
Reply | Email | Delete | Modify | 
 
 
Re: helpMahesh10/3/2008

Remove

namespace TestWebMsgApp

{

 

from the top of the class and

 

}

 

from the bottom.

Reply | Email | Delete | Modify | 
same code with more information on what the code is doingRebecca10/22/2008
http://www.developerfusion.com/code/4700/a-simple-aspnet-messagebox-class/
Reply | Email | Delete | Modify | 
Good jobGary11/7/2008
Needed a simple asp alert and this delivers. Thanks!
Reply | Email | Delete | Modify | 
WebMsgBoxPeter12/5/2008
Exactly what i need! Thank you very much
Reply | Email | Delete | Modify | 
HiThomas12/17/2008
We can add message box in ASP.net very simply.Import the windows.Forms name space and use the message box class as same in windows application Using System.Windows.Forms; If (MessageBox.Show("Do you want to proceed?", "Confirmation", MessageBoxButtons.YesNo) == DialogResult.Yes) { MessageBox.Show("You have clicked YES"); //Place your code here } else { MessageBox.Show("You have clicked NO"); //Place your code here } More details please check http://techdotnets.blogspot.com/
Reply | Email | Delete | Modify | 
 
 
Re: HiMahesh12/18/2008
Great. I know this can be done in .NET 3.0 BUT can you do this in .NET 2.0?
Reply | Email | Delete | Modify | 
really goodcurcio12/19/2008
Man, thanks a lot. You rock!
Reply | Email | Delete | Modify | 
This is really good pointTejas5/1/2009
Its good...Really Goood
Reply | Email | Delete | Modify | 
Framework 3.0 and 3.5 + AjaxMassimiliano5/5/2009
Also if with the 3.5 framwork we have a builtin msgbox we have to enabled a funciton on the webserver to enable and make it working also after we published our applications and if we have many users it can overload the server.
So i suggest to still use the javascript method to show an alert box.

Here is a way that works also insied Ajax update panels. Very easy, but you can easily implement it.

Cheers
Reply | Email | Delete | Modify | 
WebMsgBoxbob5/24/2009
nice utility works great! thanks for putting this together!
Reply | Email | Delete | Modify | 
How do I customize this messageboxMelissa7/1/2009
How do I customize this messagebox
Reply | Email | Delete | Modify |