Jquery通知弹出窗口 - jquery和updatepanels

时间:2011-06-16 11:06:51

标签: c# jquery

嗨我想要在需要时弹出通知。我正在使用一个asp.net计时器,它检查是否有新消息到达。我得到的问题是jquery通知没有出现。我猜这与更新面板有关。因为当我尝试从pageload调用它时,它运行正常。这是我的代码;

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

有人可以帮我弄清楚我做错了什么,谢谢

1 个答案:

答案 0 :(得分: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>