読者です 読者をやめる 読者になる 読者になる

ASP.NETでメッセージボックス

ASP.NET MessageBox


これはなかなか便利だ

using System;
using Microsoft.VisualBasic;
using System.Text;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebMessageBox
{
    public class WebMessageBox 
    {
        protected static Hashtable handlerPages = new Hashtable();

        private WebMessageBox() 
        {
        }

        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 msgCount = queue.Count;

                builder.Append("<script language='javascript'>");

                string message;

                while ((msgCount > 0))
                {
                    msgCount--;

                    message = System.Convert.ToString(queue.Dequeue());
                    message = message.Replace("\"", "'");

                    builder.Append("alert( \"" + message + "\" );");
                }

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

                handlerPages.Remove(HttpContext.Current.Handler);

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

}