用戶端Behavior搞定之後,我們就要藉助ASP.NET AJAX Control Toolkit提供的一大堆基礎設施,也就是基類等將其封裝成伺服器端控制項。否這給別人一個JavaScript檔案,那多不專業啊。
建立Visual Studio項目
這回我們要寫得是C#代碼,所以Visual Studio自然是少不了的。在其中建立一個類庫項目,取名為PopupNotificationExtender。然後我們要做這幾件事:
- 將編譯ASP.NET AJAX Control Toolkit得到的AjaxControlToolkit.DLL添加到項目中,拷貝到專案檔夾中也行。
- 在項目中添加該AjaxControlToolkit.DLL的引用。
- 建立PopupNotificationExtender.cs檔案。
- 建立PopupNotificationExtenderDesigner.cs檔案。
- 將前一節中編寫好的那個PopupNotificationExtenderBehavior.js檔案添加到項目中。
全部搞定之後,Visual Studio中解決方案管理器將,上面5個步驟的結果已經在圖中標出。
PopupNotificationExtender.cs檔案
搞定了準備工作以後,讓我們來編寫核心的PopupNotificationExtender.cs檔案。首先是一大堆using,注意AjaxControlToolkit這一條就行了,將ASP.NET AJAX Control Toolkit命名空間引入進來:
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.
using System;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;
using System.ComponentModel.Design;
using AjaxControlToolkit;
聲明命名空間
接著聲明命名空間,注意到前面[assembly: System.Web.UI.WebResource("Dflying.Ajax.PopupNotificationExtenderBehavior.js", "text/javascript")]這一句,用來將資源檔,也就是前面編寫的那個JavaScript檔案嵌入進去。Visual Studio中選擇PopupNotificationExtenderBehavior.js,在屬性視窗可以看到Build Action為Embedded Resource,。
[assembly: System.Web.UI.WebResource(
"Dflying.Ajax.PopupNotificationExtenderBehavior.js",
"text/javascript")]
namespace Dflying.Ajax
{
//........
}
聲明PopupNotificationExtender類
在上面的命名空間中聲明PopupNotificationExtender類,該類要繼承於AlwaysVisibleControlExtender,因為用戶端的Dflying.Ajax.PopupNotificationBehavior也是繼承於AjaxControlToolkit.AlwaysVisibleControlBehavior的:
[Designer(typeof(PopupNotificationExtenderDesigner))]
[ClientScriptResource("Dflying.Ajax.PopupNotificationBehavior", "Dflying.Ajax.PopupNotificationExtenderBehavior.js")]
[TargetControlType(typeof(Panel))]
[RequiredScript(typeof(BlockingScripts))]
public class PopupNotificationExtender : AlwaysVisibleControlExtender
{
//....
}
類的聲明很簡單,不過上面添加了一堆屬性,這裡我來簡要解釋一下:
- [Designer(typeof(PopupNotificationExtenderDesigner))]:這個用來聲明該控制項在Visual Studio中的設計器,PopupNotificationExtenderDesigner類我們一會再說。
- [ClientScriptResource("Dflying.Ajax.PopupNotificationBehavior", "Dflying.Ajax.PopupNotificationExtenderBehavior.js")]:這個用來註冊該Extender的用戶端JavaScript Behavior,注意第一個參數是用戶端的類名,第二個是資源名。請參考PopupNotificationExtenderBehavior.js檔案尾部的定義仔細書寫,一定不要寫錯。
- [TargetControlType(typeof(Panel))]:這個用來指定該Extender可以應用到什麼種類的ASP.NET伺服器端控制項上,這裡我們限定為Panel。實際開發中,朋友們可以根據情況自行選擇。
- [RequiredScript(typeof(BlockingScripts))]:BlockingScripts是ASP.NET AJAX Control Toolkit中的一個輔助指令碼,用來處理塊狀顯示地區相關的一些瀏覽器安全色問題,這裡我們需要其輔助,所以引入。
聲明Extender的各個屬性
在這裡聲明的Extender屬性均用來封裝PopupNotificationExtenderBehavior.js中定義的屬性。例如對於ServicePath屬性,其實封裝的是下面這兩個JavaScript方法(關於JavaScript部分,請參考上一篇文章):
get_ServicePath : function() {
return this._servicePath;
},
set_ServicePath : function(value) {
if (this._servicePath != value) {
this._servicePath = value;
}
},
回到PopupNotificationExtender.cs中,相應的ServicePath屬性的聲明如下:
[ExtenderControlProperty]
[DefaultValue("")]
public string ServicePath
{
get
{
return GetPropertyValue<string>("ServicePath", string.Empty);
}
set
{
SetPropertyValue<string>("ServicePath", value);
}
}
[ExtenderControlProperty]屬性用來指定這個屬性將關聯到用戶端JavaScript Behavior屬性上,起到橋樑的作用。[DefaultValue("")]用來設定預設值,這裡就是個Null 字元串。
getter和setter中的GetPropertyValue和SetPropertyValue兩個範型方法用來讀取/設定用戶端JavaScript Behavior的相應屬性值。
再舉個屬性預設值不為空白的例子:
[ExtenderControlProperty]
[DefaultValue(0.3f)]
public float ResizeEffectDuration
{
get
{
return (float)GetPropertyValue("ResizeEffectDuration", 0.3f);
}
set
{
SetPropertyValue<float>("ResizeEffectDuration", value); ;
}
}
其他各個屬性的代碼大同小異,這裡就不再列出。需要的朋友請下載源檔案自行察看。
PopupNotificationExtenderDesigner.cs檔案
設計工具檔案沒什麼好說的,現在也基本是一片空白,如果想讓你的控制項更專業的話,還是再加一些吧。限於篇幅,這裡就只留下一片空白了:
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.
using System.Web.UI.WebControls;
using System.Web.UI;
namespace Dflying.Ajax
{
class PopupNotificationExtenderDesigner : AjaxControlToolkit.Design.ExtenderControlBaseDesigner<PopupNotificationExtender>
{
}
}
到此為止,整個Extender的編寫就大功告成了!
原始碼下載
http://files.cnblogs.com/dflying/PopupNotificationExtender_source.zip
接下來一篇講講使用方法以及一些深入的、實現原理之類的東西。