2012-09-21 23 views
0

我有masterpage和它的繼承頁面。在母版頁中,我有一個字面值,我按條件顯示消息。在繼承頁面中,我使用stringbuilder創建了這些消息。
爲什麼我不能在回發中顯示一次後隱藏消息?

literal.Text = "<div id=\"alertWarning\" style=\"display:none; color:#fff; text-align:center; padding: 8px 10px 9px; width:auto; position:relative; background:#C79810;\"><h3>" + title + "</h3><p>" + error + "</p></div>"; 

      StringBuilder sb = new StringBuilder(); 
      sb.Append("$(function() { "); 
      sb.Append(" $('#alertWarning').toggle({"); 
      sb.Append(" width: 400"); 
      sb.Append(" });"); 
      sb.Append("});"); 
      sb.Append("$('html, body').animate({ scrollTop : 0 }, '500');"); 

      if (HttpContext.Current.CurrentHandler is Page) 
      { 
       Page pa = (Page)HttpContext.Current.CurrentHandler; 

       if (ScriptManager.GetCurrent(pa) != null) 
       { 
        ScriptManager.RegisterStartupScript(pa, typeof(Page), "alert", sb.ToString(), true); 
       } 
       else 
       { 
        pa.ClientScript.RegisterClientScriptBlock(typeof(Page), "alert", sb.ToString(), true); 
       } 
      } 


在母版我有此腳本自動隱藏顯示消息。

<script type="text/javascript"> 
     function pageLoad(sender, args) { 
      setTimeout(function() { $("#alertError").toggle("close"); }, 5000); 
      setTimeout(function() { $("#alertSuccess").toggle("close"); }, 2000); 
      setTimeout(function() { $("#alertWarning").toggle("close"); }, 3000); 
     }; 
    </script> 


但是,如果頁面回發顯示消息再次顯示。是否有可能只顯示一次?

回答

1

一個簡單的解決方案只會添加該腳本,如果它不是回發。

if (HttpContext.Current.CurrentHandler is Page) 
    { 
     Page pa = (Page)HttpContext.Current.CurrentHandler; 
     if(!pa.IsPostBack) 
     { 
      if (ScriptManager.GetCurrent(pa) != null) 
      { 
       ScriptManager.RegisterStartupScript(pa, typeof(Page), "alert", sb.ToString(), true); 
      } 
      else 
      { 
       pa.ClientScript.RegisterClientScriptBlock(typeof(Page), "alert", sb.ToString(), true); 
      } 
     } 
    } 
+0

也許,但我也試過,並沒有工作。現在我只是用非常愚蠢的解決方案來解決這個問題。我只是在現有的主頁上添加額外的jQuery代碼。實際上相同的代碼,但現在切換替換爲隱藏功能。 – Kadir

相關問題