coolwolf / 07/02/2023

Aspx için kod tabanlı MessageBox fonksiyonu

public class WebMsgBox
    {
        protected static Hashtable HandlerPages = new Hashtable();
        private WebMsgBox()
        {
        }
        public static void Show(string message)
        {
            if (!(HandlerPages.Contains(HttpContext.Current.Handler)))
            {
                var currentPage = (Page)HttpContext.Current.Handler;
                if (currentPage != null)
                {
                    var messageQueue = new Queue();
                    messageQueue.Enqueue(message);
                    HandlerPages.Add(HttpContext.Current.Handler, messageQueue);
                    currentPage.Unload += new EventHandler(CurrentPageUnload);
                }
            }
            else
            {
                var queue = ((Queue)(HandlerPages[HttpContext.Current.Handler]));
                queue.Enqueue(message);
            }
        }
        private static void CurrentPageUnload(object sender, EventArgs e)
        {
            var queue = ((Queue)(HandlerPages[HttpContext.Current.Handler]));
            if (queue != null)
            {
                var builder = new StringBuilder();
                int iMsgCount = queue.Count;
                builder.Append("<script language='javascript'>");
                string sMsg;
                while ((iMsgCount > 0))
                {
                    iMsgCount -= 1;
                    sMsg = 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());
            }
        }
    }

Kullanmak için, herhangi bir C# kod sayfasında (xxx.aspx.cs) alttakine benzer bir kod yazmanız yeterli. İlk açılacak Aspx formunda mesaj görüntülenecektir.

WebMsgBox.Show("Web Forms MessageBox denemesi");