Jquery Notification Popups - jquery and updatepanels
Hi i want notifications to popup if the need arises. I am using an asp.net timer which checks if a new message has arrived. The problem i am getting is that the jquery notification does not show up. I am guessing this is something to do with the update panel. Because when i tried calling it from pageload it worked fine. Here is my code;
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开发者_Python百科.ToString(), true);
}
users._bNewNotification = false;
users.UpdateNewNotification();
Cache.RemoveUserProfileCacheByUserID(users._USERS_ID);
}
}
Can someone help me figure out what it is i am doing wrong, thanks
Your approach won't work. When the browser gets your page it disconnects, so your server-side C# timer event handler code will have no browser to talk to.
You need to do something like implement polling on the web page in client-side JavaScript. e.g.
<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>
精彩评论