Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
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 :
Page Views : 301970
Downloads : 5247
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
WebMsgBox.zip
 
 
Team Foundation Server Hosting
Become a Sponsor
DevExpress Free UI Controls
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


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");

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Mahesh Chand
Mahesh is the founder of C# Corner and Mindcracker Network, an author of several .NET programming books and a Microsoft MVP for 6 consecutive years. In his day to day work, Mahesh is a Senior Software Consultant with over 14 years of IT industry experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle.  If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line 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.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
Please urgent help by vasanth On June 13, 2007
how to implement Yes/No Button in messagebox using Asp.Net.
Reply | Email | Modify 
Hiya by Sana On June 15, 2007
Suppub article............Many thx
Reply | Email | Modify 
WebMsgBox is not working with AJAX by Joyal On July 5, 2007
WebMsgBox is not working with AJAX Button. What should be the option for that?
Reply | Email | Modify 
WebMsgBox is not working with AJAX by Joyal On July 5, 2007
WebMsgBox is not working with AJAX Button. What should be the option for that?
Reply | Email | Modify 
WebMsgBox is not working with AJAX by Joyal On July 5, 2007
WebMsgBox is not working with AJAX Button. What should be the option for that?
Reply | Email | Modify 
Re: WebMsgBox is not working with AJAX by Mahesh On July 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 | Modify 
Not working by Juien On July 11, 2007
WebMsgBox is not working with WebForm Button.
Reply | Email | Modify 
Re: Not working by Mahesh On July 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 | Modify 
webmsgbox by tonygreaves1 On July 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 | Modify 
Works Great! by Luis On November 14, 2007
Thanks for the class, worked great for me. Thanks.
Reply | Email | Modify 
it works!! by anagha On December 11, 2007
Hi, Thanx a lot. gr8........
Reply | Email | Modify 
How to use by Chris On December 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 | Modify 
error near "j" by mancy On January 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 | Modify 
Re: error near "j" by Shakti Singh On March 19, 2008
Same problem I am getting
Reply | Email | Modify 
Error Message by Shakti Singh On March 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 | Modify 
Not Working for me by Joe On March 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 | Modify 
change the header by mahkameh On August 21, 2008
thank for the class, is working fine for me but how can I change the header?
Reply | Email | Modify 
change the header by mahkameh On August 21, 2008
thank for the class, is working fine for me but how can I change the header?
Reply | Email | Modify 
plz plz help by sriraj On October 1, 2008
hi mine is not a project but a website so wat namespace should i replace TestWebMsgApp with??
Reply | Email | Modify 
Re: plz plz help by Mahesh On October 1, 2008
Your web application namespace name.
Reply | Email | Modify 
help by sriraj On October 2, 2008
but i dont have any namespace for my website. now tell me how to replace the namespace???
Reply | Email | Modify 
Re: help by Mahesh On October 3, 2008

Remove

namespace TestWebMsgApp

{

 

from the top of the class and

 

}

 

from the bottom.

Reply | Email | Modify 
same code with more information on what the code is doing by Rebecca On October 22, 2008
http://www.developerfusion.com/code/4700/a-simple-aspnet-messagebox-class/
Reply | Email | Modify 
Good job by Gary On November 7, 2008
Needed a simple asp alert and this delivers. Thanks!
Reply | Email | Modify 
WebMsgBox by Peter On December 5, 2008
Exactly what i need! Thank you very much
Reply | Email | Modify 
Hi by Thomas On December 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 | Modify 
Re: Hi by Mahesh On December 18, 2008
Great. I know this can be done in .NET 3.0 BUT can you do this in .NET 2.0?
Reply | Email | Modify 
really good by curcio On December 19, 2008
Man, thanks a lot. You rock!
Reply | Email | Modify 
This is really good point by Tejas On May 1, 2009
Its good...Really Goood
Reply | Email | Modify 
Framework 3.0 and 3.5 + Ajax by Massimiliano On May 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 | Modify 
WebMsgBox by bob On May 24, 2009
nice utility works great! thanks for putting this together!
Reply | Email | Modify 
How do I customize this messagebox by Melissa On July 1, 2009
How do I customize this messagebox
Reply | Email | Modify 
WebMsgBox Working by Oke On July 28, 2009
Thank you, Mahesh Chand. This code works for me and very useful.
But i wanna ask about the web message box.
how to make a message box with buttons labeled yes, no, cancel in web form?
thank you for the response.
Reply | Email | Modify 
Escape Sequence in WebMsgBox by Ram On August 11, 2009

I want to add the escape sequence in the message box.

WebmsgBox.Show( " This is the new test " + System.Envirnoment.Newline + " Thanks");

"Thanks" is not coming in the second line... It showing the error.

Tell me how to get it in the second line.

Reply | Email | Modify 
Question about the above code by Azza On August 11, 2009
Hi,
The code worked well when I tried it .
Can anyone explain for me the above code?
Also how can I produce a warning dialouge box with yes and no buttons?

Thanks
Reply | Email | Modify 
Redirect after messagebox by Bonnie On August 22, 2009
I need to redirect after you click the "OK" on the messagebox.  Is there something I can add to the code to do this? I tried just messagebox.Show & then the redirect, but it just did the redirect. Can I install a delay?  Thank you & the code worked great!
Reply | Email | Modify 
Query by darpan On January 27, 2010
I want to replace header from "Message from Web page" to "XYZ ERROR". How can I do so?
Help me.
Reply | Email | Modify 
hashtable by prasanthi On March 29, 2010
where is the code for Hashtable.
when iam using ur code an error saying

Error 1 The type or namespace name 'Hashtable' could not be found (are you missing a using directive or an assembly reference?) 
Reply | Email | Modify 
About WebMsgBox by Luis On May 11, 2010
Wonderful article, thumbs up
Reply | Email | Modify 
web message box by anis On July 1, 2010
dear sir
thank u so much
hope you will help more  in future.
may god bless u.
Reply | Email | Modify 
Error by Brendan On July 14, 2010
I keep getting this compilation Error when tring your code Recipe.WebMsgBox.Show("This is a test");

CS0103: The name 'Recipe' does not exist in the current context

I change the namespace.
Reply | Email | Modify 
Nice Code by Rajesh On July 22, 2010
Nice code. Could you pls advise how can we implement Yes/No Confirmation Box too.
Reply | Email | Modify 
Very Nice Article by anamika On July 27, 2010
It works wonderfull!!!But i want to go on next page after clicking ok.i tried to use Response .redirect but it simply redict the page without showing message box.pls help. 
Reply | Email | Modify 
WebMsgBox by Steve On July 28, 2010
Hello Mahesh,

I downloaded your WEbMsgBox App. I added the code to my App_code Folder. I renamed the namespace to WebMsgBoxApp.

I am rewriting a web site called GAmeRoomMonitor.

I am instantiating the class using the following code:
    WebMsgBoxApp.WebMsgBox webMsg = new WebMsgBoxApp.WebMsgBox();

When I attempt to invoke the Show method  nothing is coming up in Intellisense for that method.

What am doing wrong?  Thanks for your help in advance.

Steve Stacel

Reply | Email | Modify 
hi .... by Randika On August 16, 2010
m following an IT degree.m a srilankan.
i wanna learn c# very well.but reading books and doing c# is not a good way to me...
if u can , tell me a good way to learn c# .pls help me...coz i love programming.but the thing is i dnt hv much resources.thank you very much.
Reply | Email | Modify 
Re: hi .... by Mahesh On November 8, 2010
Try Beginners link at top header of this site. It has some boks and tutorials.
Reply | Email | Modify 
Good work by Muhammad On October 13, 2010
Great work. Thanks.
Reply | Email | Modify 
sample PROJECT MAYBE.. by grace On October 14, 2010
KJL

Reply | Email | Modify 
Need VB version for my ASP.net program by Domain On November 8, 2010
Is there a version that uses VB instead of C#?
Reply | Email | Modify 
Re: Need VB version for my ASP.net program by Mahesh On November 8, 2010
Here is one article. Not sure this is exactly what you are looking for.

An easy approach to Displaying a Message Box in ASP.NET 2.0
Reply | Email | Modify 
Thanks by Mohammed On November 17, 2010
Thanks a lot for this article
Reply | Email | Modify 
About theis article by Ram On December 2, 2010
Thnx very much this article worked for me.
Reply | Email | Modify 
test ok by ALEXIN On January 15, 2011
I Tested in vb.net and work perfect , Thanks
Reply | Email | Modify 
i love this post by Linh Anh On March 16, 2011
hi,i am interested in this post.Can you give me your email address so I can exchange.I am a new programmer and look forward to learning more. Thank you very much.
Reply | Email | Modify 
Post on the Forums by Mahesh On March 16, 2011
Please post all your questions on the forums.
Reply | Email | Modify 
i want to show some message using the message box in asp.net 2.0 with c# by mercy On July 27, 2011
i used ur file.but still i am getting the error like page request manager parse error exception
Reply | Email | Modify 
sdfsd by Ravula kiran On October 4, 2011
sdsfd
Reply | Email | Modify 
thanks by moonshine On February 4, 2012
thanks a lot the code is so useful for me
Reply | Email | Modify 
Discover the top 5 tips for understanding .NET Interop
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.