2012-08-10 40 views
0

我在我的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)到另一個頁面。

任何想法爲什麼隨機功能?

+0

只是一個簡單的通話阿勒特/消息框,你說的「頁面即將獲得的意思實現這個靜態類重定向「?如果您正在將用戶發送到另一個頁面,如果此警報提示也在目標頁面上? – 2012-08-10 17:23:10

+0

不,警報只會說「您必須登錄才能訪問該信息」。我希望此警報在他們即將重定向之前彈出。 – DotNetRookie 2012-08-10 17:26:37

+0

我懷疑功能是隨機的。如果你正在做任何形式的PostBack,那麼腳本將不會被執行。它被設置爲只在您第一次訪問該頁面時執行腳本。也許你應該在實際調用Alert()方法的地方包含一些代碼。 – mclark1129 2012-08-10 17:27:43

回答

1

你可以在你的應用程序

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()); 
      } 
     } 

和呼叫像這樣MessageBox.Show("hello");