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, 2:04 AM EST
Thanks for the solution
Lucas AlmeidaPosted Sep 10, 2016, 5:55 PM EDT
Thank you very much... you saved my day.
Ramesh PalaniappanPosted Sep 10, 2016, 5:40 AM EDT
Good one
Stanislav GodinPosted Jan 27, 2015, 5:42 AM EST
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, 2:32 AM EST
its rly grt...thnks
jitendraPosted Jan 3, 2013, 9:32 PM EST
Thanks for that articles buddy
Narendran NPosted Oct 29, 2012, 11:07 PM EDT
THANKS A LOT, it rocks in ASP.NET web apps.
Ronald de FiestaeditedPosted Oct 27, 2012, 7:23 PM EDTEdited Oct 27, 2012, 7:24 PM EDT
NICE ONE... PLUS ONE FOR THE GOOD GUY!!
fiyin okPosted Aug 16, 2012, 7:34 AM EDT
Tanks Chand, web message class was quite useful. It does solved my challenge.
fiyin okPosted Aug 16, 2012, 7:33 AM EDT
Tanks Chand, web message class was quite useful. It does solved my challenge.
fiyin okPosted Aug 16, 2012, 7:33 AM EDT
Tanks Chand, web message class was quite useful. It does solved my challenge.
fiyin okPosted Aug 16, 2012, 7:33 AM EDT
Tanks Chand, web message class was quite useful. It does solved my challenge.
nop vongPosted Apr 24, 2012, 8:13 AM EDT
Thank you very much. It's very useful . Nice Code.
ramakrishna muvvalaPosted Apr 9, 2012, 1:54 AM EDT
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, 11:21 AM ESTEdited Feb 4, 2012, 11:23 AM EST
thanks a lot the code is so useful for me
Ravula kiranPosted Oct 3, 2011, 11:32 PM EDT
sdsfd
mercy manickamPosted Jul 27, 2011, 3:08 AM EDT
i used ur file.but still i am getting the error like page request manager parse error exception
Linh Anh HoangPosted Mar 16, 2011, 12:10 AM EDT
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 14, 2011, 7:50 PM EST
I Tested in vb.net and work perfect , Thanks
Ram ChapagainPosted Dec 2, 2010, 2:00 AM EST
Thnx very much this article worked for me.
Mohammed ElSaidPosted Nov 17, 2010, 11:40 AM EST
Thanks a lot for this article
Domain UserPosted Nov 8, 2010, 6:36 AM EST
Is there a version that uses VB instead of C#?
gracePosted Oct 14, 2010, 2:33 AM EDT
KJL
Muhammad ShakirPosted Oct 13, 2010, 1:24 AM EDT
Great work. Thanks.
Randika FernandoPosted Aug 16, 2010, 6:52 PM EDT
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, 8:14 AM EDT
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, 1:28 AM EDTEdited Jul 27, 2010, 2:12 AM EDT
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, 12:36 AM EDT
Nice code. Could you pls advise how can we implement Yes/No Confirmation Box too.
Brendan NguyenPosted Jul 14, 2010, 7:08 AM EDT
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 Jun 30, 2010, 8:17 PM EDT
dear sir thank u so much hope you will help more in future. may god bless u.
Luis PinedaPosted May 11, 2010, 11:05 AM EDT
Wonderful article, thumbs up
prasanthiPosted Mar 29, 2010, 12:13 PM EDT
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, 8:40 AM EST
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, 1:56 PM EDT
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, 2:15 PM EDT
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, 12:07 PM EDT
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, 7:17 AM EDT
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, 12:21 PM EDT
How do I customize this messagebox
bob kennellyPosted May 24, 2009, 7:05 AM EDT
nice utility works great! thanks for putting this together!
MassimilianoeditedPosted May 5, 2009, 8:37 AM EDTEdited May 5, 2009, 8:38 AM EDT
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, 4:03 AM EDT
Its good...Really Goood
curcioPosted Dec 19, 2008, 10:37 AM EST
Man, thanks a lot. You rock!
Thomas MathewPosted Dec 17, 2008, 6:56 PM EST
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, 8:32 AM EST
Exactly what i need! Thank you very much
Gary GreenPosted Nov 7, 2008, 8:20 AM EST
Needed a simple asp alert and this delivers. Thanks!
RebeccaPosted Oct 22, 2008, 1:20 PM EDT
http://www.developerfusion.com/code/4700/a-simple-aspnet-messagebox-class/
sriraj nairPosted Oct 2, 2008, 3:54 PM EDT
but i dont have any namespace for my website. now tell me how to replace the namespace???
sriraj nairPosted Oct 1, 2008, 6:30 PM EDT
hi mine is not a project but a website so wat namespace should i replace TestWebMsgApp with??
mahkamehPosted Aug 21, 2008, 9:13 AM EDT
thank for the class, is working fine for me but how can I change the header?
mahkamehPosted Aug 21, 2008, 9:13 AM EDT
thank for the class, is working fine for me but how can I change the header?
Joe MPosted Mar 19, 2008, 7:58 AM EDT
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 18, 2008, 9:16 PM EDT
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, 2:53 AM EST
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, 6:02 AM EST
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 10, 2007, 8:58 PM EST
Hi, Thanx a lot. gr8........
LuisPosted Nov 14, 2007, 5:17 AM EST
Thanks for the class, worked great for me. Thanks.
tonygreaves1Posted Jul 15, 2007, 3:51 PM EDT
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, 12:15 AM EDT
WebMsgBox is not working with WebForm Button.
JoyalPosted Jul 5, 2007, 5:39 AM EDT
WebMsgBox is not working with AJAX Button. What should be the option for that?
JoyalPosted Jul 5, 2007, 5:38 AM EDT
WebMsgBox is not working with AJAX Button. What should be the option for that?
JoyalPosted Jul 5, 2007, 5:21 AM EDT
WebMsgBox is not working with AJAX Button. What should be the option for that?
Sana SarjPosted Jun 15, 2007, 2:25 AM EDT
Suppub article............Many thx
vasanth rajPosted Jun 13, 2007, 3:43 AM EDT
how to implement Yes/No Button in messagebox using Asp.Net.