前几天有朋友说希望能用ASP.NET AJAX实现类似OWA或Messenger样式的信息提示窗口,当系统有新消息的时候,可以在屏幕右下角弹出一个提示面板,其中放置自定义的消息。就像下面图示的这样:
今天上午抽时间作了一个,以ASP.NET AJAX Control Toolkit Extender的形式发布。限于HTTP协议的局限性,只能采取客户端pull的方法,每隔一段时间查询一下某个Web Service,如果有新消息,则在客户端以动画形式显示出来。如下面两张图,左边的刚刚显示一半,右边已经完整显示了出来(点击查看大图):
PopupNotificationExtender功能介绍
PopupNotificationExtender下载
下载、使用本软件之前,请仔细阅读如下Microsoft Permissive License (Ms-PL)版权协议。如果你使用本软件,说明你无条件接受该协议中的条款。如果你不接受该协议,请不要使用本软件。
This license governs use of the accompanying software. If you use the software, you accept this license. If you do not accept the license, do not use the software.
The terms “reproduce,” “reproduction” and “distribution” have the same meaning here as under U.S. copyright law.
“You” means the licensee of the software.
“Licensed patents” means any Microsoft patent claims which read directly on the software as distributed by Microsoft under this license.
(A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, Microsoft grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce the software, prepare derivative works of the software and distribute the software or any derivative works that you create.
(B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, Microsoft grants you a non-exclusive, worldwide, royalty-free patent license under licensed patents to make, have made, use, practice, sell, and offer for sale, and/or otherwise dispose of the software or derivative works of the software.
(A) No Trademark License- This license does not grant you any rights to use Microsoft’s name, logo, or trademarks.
(B) If you begin patent litigation against Microsoft over patents that you think may apply to the software (including a cross-claim or counterclaim in a lawsuit), your license to the software ends automatically.
(C) If you distribute copies of the software or derivative works, you must retain all copyright, patent, trademark, and attribution notices that are present in the software.
(D) If you distribute the software or derivative works in source code form you may do so only under this license (i.e., you must include a complete copy of this license with your distribution), and if you distribute the software or derivative works in compiled or object code form you may only do so under a license that complies with this license.
(E) The software is licensed “as-is.” You bear the risk of using it. Microsoft gives no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, Microsoft excludes the implied warranties of merchantability, fitness for a particular purpose and non-infringement.
PopupNotificationExtender示例程序
本控件基于ASP.NET AJAX开发,且继承于ASP.NET AJAX Control Toolkit中的AlwaysVisibleControlExtender。所以若要在程序中使用该控件,则必须配置好ASP.NET AJAX并添加好ASP.NET AJAX Control Toolkit程序集的引用(请参考《拥抱变化——从Atlas到ASP.NET AJAX(1):下载安装总览》)。
然后将控件的DLL(Dflying.Ajax.PopupNotificationExtender.zip)解压缩至Web站点的bin目录下,添加好对该DLL的引用。
在需要使用的页面头部添上如下注册代码:
<%@ Register Assembly="Dflying.Ajax.PopupNotificationExtender" Namespace="Dflying.Ajax"
TagPrefix="dflying" %>
当然,ScriptManager也是必须的:
<asp:ScriptManager ID="ScriptManager1" runat="server" />
然后定义一个Panel,用来表示提示窗口,当然其中布局样式朋友们可以随心所欲地改变:
<asp:Panel ID="thePanel" CssClass="panel" runat="server">
<div style="border: 1px solid black; height: 98px;">
<div style="padding: 3px; background-color: Silver;">
<strong>New Messages:strong>
div>
<img src="icon.gif" style="float: left; display: block; margin: 3px;" />
<div id="result" style="padding: 3px; margin-left: 40px;" />
div>
asp:Panel>
注意:该Panel中还包含了一个id为result的HTML
该Panel应用的CSS Class为panel,定义如下:(注意不可以定义border、margin、padding三个属性,如果需要,可以在内部标签
.panel
{
font-size: 80%;
background-color: white;
width: 200px;
height: 100px;
overflow: hidden;
}
然后是PopupNotificationExtender控件的代码:
<dflying:PopupNotificationExtender ID="pne" TargetControlID="thePanel" runat="server"
VerticalSide="Bottom" HorizontalSide="Right" HorizontalOffset="20" VerticalOffset="20"
ServicePath="NotificationService.asmx" ServiceMethod="GetNotification" QueryServiceInterval="6000"
ResultLabelID="result" />
其中:
再看看服务器端Web Service的代码:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[Microsoft.Web.Script.Services.ScriptService()]
public class NotificationService : System.Web.Services.WebService {
private static int m_count = 0;
[WebMethod]
public string GetNotification()
{
if (checkNewMessage())
{
// return the HTML message content.
return string.Format("You've received a new message #{0}.", m_count++);
}
else
{
// if there's no new meesage, just return an empty string.
return string.Empty;
}
}
private bool checkNewMessage()
{
// TODO: whatever you want to check if there's a message.
return true;
}
}
很简单不多说了,GetNotification()方法没有任何传入参数,在该方法中,我们可以随便用什么方法看看是否有新的消息需要传递给客户端。如果有的话,那么返回代表该消息的HTML字符串,如果没有,则返回空字符串即可。之后客户端如果收到的是一个非空字符串,则将弹出窗口显示出来,如果受到空字符串,那么不会显示任何东西。
程序运行界面就和本文开始的两幅图像一样,你也可以下载示例程序(PopupNotificationTest.zip)亲自体验一下。
PopupNotificationExtender属性列表
其他
http://www.cnblogs.com/dflying/archive/2006/11/29/576558.html