从代码隐藏中以 asp.net 显示警报框(并不总是有效)



我有一个通用的警报函数,在我的 asp.net 应用程序中显示一个消息框:

public void Alert(string sTitle, string sMessage)
{
  StringBuilder sbScript = new StringBuilder();
  sbScript.Append("<script language='Javascript'>");
  sbScript.Append("var varDateNow = new Date();");
  sbScript.Append("var varTimeNow = varDateNow.getTime();");
  //sbScript.Append("var varAlertTime = document.getElementById('Master_cphAlertTime').value;");        
  sbScript.Append("var varAlertTime = document.getElementById('cphAlertTime').value;");
  sbScript.Append("if(varTimeNow - varAlertTime < 1500)");
  sbScript.Append("{alert('");
  sbScript.Append(strMessage);
  sbScript.Append("');}");
  sbScript.Append("</script>");
  ClientScript.RegisterStartupScript(this.GetType(), strTitle, sbScript.ToString());
}   

"警报"框并非每次都显示。让我感到困惑的是,为什么它有时会出现,而在其他时间却没有出现?它没有出现的时间是当一个页面即将被重定向(或server.transfer)到另一个页面时。

任何想法为什么随机功能?

您可以在应用程序中实现此静态类

public class MessageBox
{
        private static Hashtable m_executingPages = new Hashtable();
        private MessageBox()
        { }
        public static void Show(string sMessage)
        {
            // If this is the first time a page has called this method then
            if (!m_executingPages.Contains(HttpContext.Current.Handler))
            {
                // Attempt to cast HttpHandler as a Page.
                Page executingPage = HttpContext.Current.Handler as Page;
                if (executingPage != null)
                {
                    // Create a Queue to hold one or more messages.
                    Queue messageQueue = new Queue();
                    // Add our message to the Queue
                    messageQueue.Enqueue(sMessage);
                    // Add our message queue to the hash table. Use our page reference
                    // (IHttpHandler) as the key.
                    m_executingPages.Add(HttpContext.Current.Handler, messageQueue);
                    // Wire up Unload event so that we can inject 
                    // some JavaScript for the alerts.
                    executingPage.Unload += new EventHandler(ExecutingPage_Unload);
                }
            }
            else
            {
                // If were here then the method has allready been 
                // called from the executing Page.
                // We have allready created a message queue and stored a
                // reference to it in our hastable. 
                Queue queue = (Queue)m_executingPages[HttpContext.Current.Handler];
                // Add our message to the Queue
                queue.Enqueue(sMessage);
            }
        }
        // Our page has finished rendering so lets output the
        // JavaScript to produce the alert's
        private static void ExecutingPage_Unload(object sender, EventArgs e)
        {
            // Get our message queue from the hashtable
            Queue queue = (Queue)m_executingPages[HttpContext.Current.Handler];
            if (queue != null)
            {
                StringBuilder sb = new StringBuilder();
                // How many messages have been registered?
                int iMsgCount = queue.Count;
                // Use StringBuilder to build up our client slide JavaScript.
                sb.Append("<script language='javascript'>");
                // Loop round registered messages
                string sMsg;
                while (iMsgCount-- > 0)
                {
                    sMsg = (string)queue.Dequeue();
                    sMsg = sMsg.Replace("n", "\n");
                    sMsg = sMsg.Replace(""", "'");
                    sb.Append(@"alert( """ + sMsg + @""" );");
                }
                // Close our JS
                sb.Append(@"</script>");
                // Were done, so remove our page reference from the hashtable
                m_executingPages.Remove(HttpContext.Current.Handler);
                // Write the JavaScript to the end of the response stream.
                HttpContext.Current.Response.Write(sb.ToString());
            }
        }

并且只需像这样一个简单的调用即可调用Allert/消息框MessageBox.Show("hello");

相关内容

  • 没有找到相关文章

最新更新