2011-06-16 120 views
0

嗨,我想通知彈出如果需要出現。我正在使用一個asp.net計時器來檢查是否有新消息到達。我得到的問題是,jquery通知不顯示。我猜這是與更新面板有關的事情。因爲當我試圖從頁面加載調用它時,它工作正常。這是我的代碼;jquery通知彈出窗口 - jquery和updatepanels

protected void updateTimer_OnTick(object sender, EventArgs e) 
    { 
     Cache Cache = new Cache(); 

     users = Cache.getUserDetailsByUserID(Convert.ToInt32(Page.User.Identity.Name)); 

     if (users._bNewNotification) 
     { 
      List<UserNotification> listUserNotification = null; 

      listUserNotification = Cache.getLatestNotifcationsByUserID(Convert.ToInt32(Page.User.Identity.Name)); 

      foreach (UserNotification userNotification in listUserNotification) 
      { 
       StringBuilder jquery2 = new StringBuilder(); 

       jquery2.AppendLine("$.extend($.gritter.options, {"); 
       jquery2.AppendLine("position: 'bottom-left',"); 
       jquery2.AppendLine("fade_in_speed: 100,"); 
       jquery2.AppendLine("fade_out_speed: 100,"); 
       jquery2.AppendLine("time: 3000"); 
       jquery2.AppendLine("});"); 
       Page.ClientScript.RegisterStartupScript(typeof(Page), Guid.NewGuid().ToString(), jquery2.ToString(), true); 

       StringBuilder jquery1 = new StringBuilder(); 
       jquery1.AppendLine(" var unique_id = $.gritter.add({"); 
       jquery1.AppendLine("  title: 'This is a sticky notice!',"); 
       jquery1.AppendLine("  text: '" + userNotification._NotificationType + "',"); 
       jquery1.AppendLine("  image: 'http://s3.amazonaws.com/twitter_production/profile_images/132499022/myface_bigger.jpg',"); 
       jquery1.AppendLine("  sticky: true,"); 
       jquery1.AppendLine("  time: '',"); 
       jquery1.AppendLine("  class_name: 'my-sticky-class'"); 
       jquery1.AppendLine(" });"); 
       Page.ClientScript.RegisterStartupScript(typeof(Page), Guid.NewGuid().ToString(), jquery1.ToString(), true); 
      } 

      users._bNewNotification = false; 
      users.UpdateNewNotification(); 

      Cache.RemoveUserProfileCacheByUserID(users._USERS_ID); 
     } 
    } 

有人可以幫助我弄清楚它是什麼,我做錯了,感謝

+0

你看過一個Javascript調試器/瀏覽器控制檯來檢查運行時錯誤嗎? – foxy 2011-06-16 11:35:44

+0

是的,沒有錯誤被拋出。我需要重新加載我的JavaScript文件,因爲我知道該函數依賴於正在加載的文件? – pmillio 2011-06-16 11:49:51

回答

2

你的做法是行不通的。當瀏覽器得到你的頁面時,它會斷開連接,所以你的服務器端的C#定時器事件處理程序代碼將沒有瀏覽器可以與之通話。

您需要做一些工作,如在客戶端 JavaScript中的網頁上執行輪詢。例如

<script> 
$(document).ready(function(){ 
    window.setInterval(function(){ 
     $.get('path/to/GetNotifications.aspx', function(data){ 
      // data contains text from GetNotifications.aspx 
      // it could be JSON, XML, CSV... it's up to you 
      // do something with it here... 
     }) 
    },5000 /*5s*/) 
}) 
</script> 
+0

嗨感謝您的迴應,我想返回上面我的stringbuilder中的jquery,我該如何去做,如果聽起來像一個愚蠢的問題抱歉。 – pmillio 2011-06-16 13:36:58

+0

就像我說的:你的方法不行。您只需將客戶端輪詢代碼嵌入到ASPX頁面中,然後編寫一個「服務」頁面,爲您檢索通知。 – 2011-06-17 16:07:07