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

bhupender kumarPosted Dec 4, 2018, 7:04 AM
Thanks for the solution
Lucas AlmeidaPosted Sep 10, 2016, 9:55 PM
Thank you very much... you saved my day.
Ramesh PalaniappanPosted Sep 10, 2016, 9:40 AM
Good one
Stanislav GodinPosted Jan 27, 2015, 10:42 AM
I changed the namespace to namespace TestWebMsgApp. I can't call the WebMsgBox. Do I need to reference the namespace?
Foysal AlanPosted Jan 8, 2013, 7:32 AM
its rly grt...thnks
jitendraPosted Jan 4, 2013, 2:32 AM
Thanks for that articles buddy
Narendran NPosted Oct 30, 2012, 3:07 AM
THANKS A LOT, it rocks in ASP.NET web apps.
Ronald de FiestaeditedPosted Oct 27, 2012, 11:23 PMEdited Oct 27, 2012, 11:24 PM
NICE ONE... PLUS ONE FOR THE GOOD GUY!!
fiyin okPosted Aug 16, 2012, 11:34 AM
Tanks Chand, web message class was quite useful. It does solved my challenge.
fiyin okPosted Aug 16, 2012, 11:33 AM
Tanks Chand, web message class was quite useful. It does solved my challenge.
fiyin okPosted Aug 16, 2012, 11:33 AM
Tanks Chand, web message class was quite useful. It does solved my challenge.
fiyin okPosted Aug 16, 2012, 11:33 AM
Tanks Chand, web message class was quite useful. It does solved my challenge.
nop vongPosted Apr 24, 2012, 12:13 PM
Thank you very much. It's very useful . Nice Code.
ramakrishna muvvalaPosted Apr 9, 2012, 5:54 AM
hi ,u r article is very helpful for me,But how can i use web message box with more than one button(yes,no,cancel)
moonshine moonshineeditedPosted Feb 4, 2012, 4:21 PMEdited Feb 4, 2012, 4:23 PM
thanks a lot the code is so useful for me
Ravula kiranPosted Oct 4, 2011, 3:32 AM
sdsfd
mercy manickamPosted Jul 27, 2011, 7:08 AM
i used ur file.but still i am getting the error like page request manager parse error exception
Linh Anh HoangPosted Mar 16, 2011, 4:10 AM
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.
ALEXINPosted Jan 15, 2011, 12:50 AM
I Tested in vb.net and work perfect , Thanks
Ram ChapagainPosted Dec 2, 2010, 7:00 AM
Thnx very much this article worked for me.
Mohammed ElSaidPosted Nov 17, 2010, 4:40 PM
Thanks a lot for this article
Domain UserPosted Nov 8, 2010, 11:36 AM
Is there a version that uses VB instead of C#?
gracePosted Oct 14, 2010, 6:33 AM
KJL
Muhammad ShakirPosted Oct 13, 2010, 5:24 AM
Great work. Thanks.
Randika FernandoPosted Aug 16, 2010, 10:52 PM
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.
Steve StacelPosted Jul 28, 2010, 12:14 PM
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
anamika singheditedPosted Jul 27, 2010, 5:28 AMEdited Jul 27, 2010, 6:12 AM
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.
Rajesh KumarPosted Jul 22, 2010, 4:36 AM
Nice code. Could you pls advise how can we implement Yes/No Confirmation Box too.
Brendan NguyenPosted Jul 14, 2010, 11:08 AM
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.
anis ahmedPosted Jul 1, 2010, 12:17 AM
dear sir thank u so much hope you will help more in future. may god bless u.
Luis PinedaPosted May 11, 2010, 3:05 PM
Wonderful article, thumbs up
prasanthiPosted Mar 29, 2010, 4:13 PM
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?)
darpan sanchelaPosted Jan 27, 2010, 1:40 PM
I want to replace header from "Message from Web page" to "XYZ ERROR". How can I do so? Help me.
Bonnie SmithPosted Aug 22, 2009, 5:56 PM
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!
AzzaPosted Aug 11, 2009, 6:15 PM
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
Ram NPosted Aug 11, 2009, 4:07 PM
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.
Oke UmarPosted Jul 28, 2009, 11:17 AM
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.
Melissa MasizaPosted Jul 1, 2009, 4:21 PM
How do I customize this messagebox
bob kennellyPosted May 24, 2009, 11:05 AM
nice utility works great! thanks for putting this together!
MassimilianoeditedPosted May 5, 2009, 12:37 PMEdited May 5, 2009, 12:38 PM
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
Tejas KareliyaPosted May 1, 2009, 8:03 AM
Its good...Really Goood
curcioPosted Dec 19, 2008, 3:37 PM
Man, thanks a lot. You rock!
Thomas MathewPosted Dec 17, 2008, 11:56 PM
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/
Peter BollingerPosted Dec 5, 2008, 1:32 PM
Exactly what i need! Thank you very much
Gary GreenPosted Nov 7, 2008, 1:20 PM
Needed a simple asp alert and this delivers. Thanks!
RebeccaPosted Oct 22, 2008, 5:20 PM
http://www.developerfusion.com/code/4700/a-simple-aspnet-messagebox-class/
sriraj nairPosted Oct 2, 2008, 7:54 PM
but i dont have any namespace for my website. now tell me how to replace the namespace???
sriraj nairPosted Oct 1, 2008, 10:30 PM
hi mine is not a project but a website so wat namespace should i replace TestWebMsgApp with??
mahkamehPosted Aug 21, 2008, 1:13 PM
thank for the class, is working fine for me but how can I change the header?
mahkamehPosted Aug 21, 2008, 1:13 PM
thank for the class, is working fine for me but how can I change the header?
Joe MPosted Mar 19, 2008, 11:58 AM
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;
Shakti Singh DulawatPosted Mar 19, 2008, 1:16 AM
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
mancy mancyPosted Jan 19, 2008, 7:53 AM
please help when ever the Show("Your message here"); is called an error message appears error near "j" tell me what to do??? thanks
Chris AndersonPosted Dec 30, 2007, 11:02 AM
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
anagha joshiPosted Dec 11, 2007, 1:58 AM
Hi, Thanx a lot. gr8........
LuisPosted Nov 14, 2007, 10:17 AM
Thanks for the class, worked great for me. Thanks.
tonygreaves1Posted Jul 15, 2007, 7:51 PM
After uploading my web app to production the webmsgbox produces a permissions errors from user's desktop. How can I correct this?
Juien MondalPosted Jul 11, 2007, 4:15 AM
WebMsgBox is not working with WebForm Button.
JoyalPosted Jul 5, 2007, 9:39 AM
WebMsgBox is not working with AJAX Button. What should be the option for that?
JoyalPosted Jul 5, 2007, 9:38 AM
WebMsgBox is not working with AJAX Button. What should be the option for that?
JoyalPosted Jul 5, 2007, 9:21 AM
WebMsgBox is not working with AJAX Button. What should be the option for that?
Sana SarjPosted Jun 15, 2007, 6:25 AM
Suppub article............Many thx
vasanth rajPosted Jun 13, 2007, 7:43 AM
how to implement Yes/No Button in messagebox using Asp.Net.