OWA或Messenger样式的信息提示窗口(中)服务器端控件

来源:互联网 发布:淘宝主图尺寸 编辑:程序博客网 时间:2024/05/22 01:48

客户端Behavior搞定之后,我们就要借助ASP.NET AJAX Control Toolkit提供的一大堆基础设施,也就是基类等将其封装成服务器端控件。否这给别人一个JavaScript文件,那多不专业啊。

 

创建Visual Studio项目

这回我们要写得是C#代码,所以Visual Studio自然是少不了的。在其中新建一个类库项目,取名为PopupNotificationExtender。然后我们要做这几件事:

  1. 将编译ASP.NET AJAX Control Toolkit得到的AjaxControlToolkit.DLL添加到项目中,拷贝到项目文件夹中也行。
  2. 在项目中添加该AjaxControlToolkit.DLL的引用。
  3. 创建PopupNotificationExtender.cs文件。
  4. 创建PopupNotificationExtenderDesigner.cs文件。
  5. 将前一节中编写好的那个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;
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

 

声明命名空间

接着声明命名空间,注意到前面[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
{
 //....
}

类的声明很简单,不过上面添加了一堆属性,这里我来简要解释一下:

  1. [Designer(typeof(PopupNotificationExtenderDesigner))]:这个用来声明该控件在Visual Studio中的设计器,PopupNotificationExtenderDesigner类我们一会再说。
  2. [ClientScriptResource("Dflying.Ajax.PopupNotificationBehavior", "Dflying.Ajax.PopupNotificationExtenderBehavior.js")]:这个用来注册该Extender的客户端JavaScript Behavior,注意第一个参数是客户端的类名,第二个是资源名。请参考PopupNotificationExtenderBehavior.js文件尾部的定义仔细书写,一定不要写错。
  3. [TargetControlType(typeof(Panel))]:这个用来指定该Extender可以应用到什么种类的ASP.NET服务器端控件上,这里我们限定为Panel。实际开发中,朋友们可以根据情况自行选择。
  4. [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("")]用来设置默认值,这里就是个空字符串。

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://www.cnblogs.com/Files/dflying/PopupNotificationExtender_source.zip

接下来一篇讲讲使用方法以及一些深入的、实现原理之类的东西。

备注:本文引用自http://www.cnblogs.com/dflying/archive/2007/03/02/661803.html