我的ASP.NET AJAX控件——PopupNotificationExtender:实现OWA或Messenger样式的信息提示窗口...
前幾天有朋友說希望能用ASP.NET AJAX實現類似OWA或Messenger樣式的信息提示窗口,當系統有新消息的時候,可以在屏幕右下角彈出一個提示面板,其中放置自定義的消息。就像下面圖示的這樣:
今天上午抽時間作了一個,以ASP.NET AJAX Control Toolkit Extender的形式發布。限于HTTP協議的局限性,只能采取客戶端pull的方法,每隔一段時間查詢一下某個Web?Service,如果有新消息,則在客戶端以動畫形式顯示出來。如下面兩張圖,左邊的剛剛顯示一半,右邊已經完整顯示了出來(點擊查看大圖):
??????
?
PopupNotificationExtender功能介紹
?
PopupNotificationExtender下載
下載、使用本軟件之前,請仔細閱讀如下Microsoft Permissive License (Ms-PL)版權協議。如果你使用本軟件,說明你無條件接受該協議中的條款。如果你不接受該協議,請不要使用本軟件。
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.
1. Definitions
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.
2. Grant of Rights
(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.
3. Conditions and Limitations
(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 <div>標簽。注意這個<div>,等會服務器端返回的消息將填充到該<div>中。
該Panel應用的CSS Class為panel,定義如下:(注意不可以定義border、margin、padding三個屬性,如果需要,可以在內部標簽<div>中使用)
.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("<a href=\"#\">You've received a new message #{0}.</a>", 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屬性列表
?
其他
總結
以上是生活随笔為你收集整理的我的ASP.NET AJAX控件——PopupNotificationExtender:实现OWA或Messenger样式的信息提示窗口...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ArcGIS特殊标注效果的简单实现
- 下一篇: 安装Xcode在Mac OS X10.7