2012-09-17 35 views
0

我有一個單選按鈕(在updatepanel中)和checkchanged事件我正在向用戶顯示一條消息。但div在masterpage中,頁面沒有渲染,這就是爲什麼我不能顯示消息。 可以解決這個問題嗎?順便說一句我正在使用類來顯示消息。 這是我的代碼。爲什麼我無法在類的updatepanel頁面中顯示js錯誤警報?

public static void setError(System.Web.UI.Page p, string title, string error, string type) 
    { 
     Literal literal = p.Master.FindControl("ltMessage") as Literal; 

     if (type == "error") 
     { 
      literal.Text = "<div id=\"alertError\" style=\"display:none; color:#fff; text-align:center; padding: 8px 10px 9px; width:auto; position:relative; background:#cc0000;\"><h3>" + title + "</h3><p>" + error + "</p></div>"; 
      //literal.Text = "<div id=\"alert\" class=\"error message\"><h3>" + title + "</h3><p>" + error + "</p></div>"; 

      StringBuilder sb = new StringBuilder(); 
      sb.Append("$(function() { "); 
      sb.Append(" $('#alertError').toggle({"); 
      sb.Append(" width: 400"); 
      sb.Append(" });"); 
      sb.Append("});"); 

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

回答

0

我修好了。我把我的div放在主頁面的updatepanel中。現在它完美。

1

不確定你是如何調用setError的。但$(function() {...})$(document).ready(function(){...})的同義詞,僅在加載頁面後調用。 UpadtePanel正在進行AJAX調用,並且不會運行$(document).ready事件。試試下面的代碼:

StringBuilder sb = new StringBuilder(); 
sb.Append(" $('#alertError').toggle({"); 
sb.Append(" width: 400"); 
sb.Append(" });"); 

這應該工作,如果你正在做正確的一切。

相關問題