Popupwin结合Timer实现定时弹出消息提示
目前开发的项目中需要实现到发送消息提醒的功能,在网上找到了这款仿MSN的消息控件,感觉它还是可以满足我们使用要求的,就拿过来用了,现在把使用的过程及心得体会写下来,供需要的人参考。
首先下载EeekSoft.Web.PopupWin.dll,将其添加到项目的引用中之后,然后在前台添加代码:
<%@ Register Assembly="EeekSoft.Web.PopupWin" Namespace="EeekSoft.Web" TagPrefix="cc1" %>
就可以使用该控件了。
如果想要实现在打开的网页中的定时异步刷新,则可以结合使用Asp.net控件Timer的UpdatePanel,分别将Timer和Popupwin放在一个UpdatePanel中,UpdateMode属性设置为Conditional,这里简单的介绍一下UpdatePanel的属性,它共有两个属性值:
always:页面上的任何控件的操作都会引起对UpdatePanel中的内容的更新;
conditional:只有UpdatePanel中的控件的操作才会引起UpdatePanel中的内容的更新。
这样就大概能理解将UpdateMode属性设为Conditional的用意了吧, 即不能让页面上任何一个对任何一个控件的操作都引起Timer的Popupwin的刷新。
前台代码如下:
1 <asp:ScriptManager runat="server" ID="sm"></asp:ScriptManager> 2 <div> 3 <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" > 4 <ContentTemplate> 5 <asp:Timer ID="Timer" runat="server" ontick="Timer_Tick" Interval="6000" Enabled="true"> 6 </asp:Timer> 7 </ContentTemplate> 8 </asp:UpdatePanel> 9 <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional" > 10 <ContentTemplate> 11 <cc1:popupwin id="pw" runat="server" style="left: 63px; top: 99px" ActionType="RaiseEvents" HideAfter="-1" ShowAfter="500" 12 ShowLink="true" AutoShow="true" Visible="true" OnLinkClicked="pw_LinkClick" /> 13 </ContentTemplate> 14 </asp:UpdatePanel> 15 </div>
后台代码如下:
protected void Timer_Tick(object sender, EventArgs e) { ScriptManager.RegisterStartupScript(this.UpdatePanel2, this.UpdatePanel2.GetType(), "msg", "pwespopup_winLoad();", true); //读取数据库中未提醒信息,根据需要拼接成字符串赋值给popupwin的Message属性 string msg = "<table boder='0' cellspacing='0' cellpadding='3' class='small'>"; msg += "<tr><td align='center'><a href=\"javascript:ReadMsg('AllMsgs')\">您有" + unReadCount.ToString() + "条未读提醒</a></td></tr>"; foreach (DictionaryEntry de in ht) { msg += "<tr><td><a href=\"javascript:ReadMsg('" + de.Key + "')\"><img alt='' src='" + htImg[de.Key] + "' />" +de.Key + "(" + de.Value + ")条</a><td><tr>"; } msg += "</table>"; pw.Message = msg; pw.Title = "消息提醒"; pw.ColorStyle = EeekSoft.Web.PopupColorStyle.Blue; pw.DragDrop = true; pw.Width = 200; pw.Height = 100 + ht.Count * 10; pw.DockMode = EeekSoft.Web.PopupDocking.BottomLeft; this.UpdatePanel2.Update(); } } protected void pw_LinkClick(object sender, EventArgs e) { }
最后的效果图如下:
选中其中一行文本都可以执行相应的前台js脚本函数。