随笔-313  评论-12138  文章-1  trackbacks-256

OWA或Messenger样式的信息提示窗口——编写ASP.NET AJAX Extender控件(中):封装成服务器端控件

客户端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;

 

声明命名空间

接着声明命名空间,注意到前面[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://files.cnblogs.com/dflying/PopupNotificationExtender_source.zip

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

posted on 2007-03-02 15:31 Dflying Chen 阅读(5346) 评论(24) 编辑 收藏

评论:
#1楼 2007-03-02 16:05 | aspnetx      
好久不见Dflying Chen的文章了
仿佛又回到了三个月(?)前,呵呵,有三个月了吧

 回复 引用 查看   
#2楼 2007-03-02 16:53 | 郑海恭[未注册用户]
那你这个如何使用呢????
 回复 引用   
#3楼 2007-03-02 21:11 | yukaizhao[未注册用户]
研究了一些,ms的ajax extension搞得太复杂了,实现一个功能,没有必要搞那么复杂的。不过好处也是不言而喻的。
 回复 引用   
#4楼 2007-03-02 23:01 | Clingingboy      
都是为了重用,不然怎么会搞这么复杂
 回复 引用 查看   
#5楼 2007-03-03 00:28 | Jeffrey Zhao      
@Clingingboy
其实是为了在服务器端操作,客户端有Behavior其实已经足够了。
还有,有了Extender,往往很多朋友都会忽略了客户端是Behavior的事实,其实很多操作可以在客户端作。

 回复 引用 查看   
#6楼[楼主] 2007-03-03 01:33 | Dflying Chen      
@aspnetx
不会吧?我不是一直在写么……

 回复 引用 查看   
#7楼[楼主] 2007-03-03 01:34 | Dflying Chen      
@郑海恭
下一片将给出使用方法。

 回复 引用 查看   
#8楼[楼主] 2007-03-03 01:34 | Dflying Chen      
@yukaizhao
@Clingingboy
@Jeffrey Zhao
使用方便就行,呵呵,开发麻烦也无所谓

 回复 引用 查看   
#9楼 2007-03-05 09:02 | Frank[未注册用户]
好文!下一篇热切期待中!
 回复 引用   
#10楼 2007-03-05 09:33 | 笑疯^_^      
开心学习中啊...
 回复 引用 查看   
#11楼 2007-03-05 09:47 | Min.W
欢迎回来!我们又见到了几个月前的你,真的,那个熟悉的背影,希望这次别再走开了!
 回复 引用   
#12楼 2007-03-06 16:00 | xyun[未注册用户]
期待下篇!
 回复 引用   
#13楼 2007-03-07 10:41 | xyun[未注册用户]
如何修改可以使得该控件可以跨框架啊?
同样的问题也出现在很多ms ajax控件中。

 回复 引用   
#14楼 2007-03-07 10:52 | 郑海恭[未注册用户]
很是期待下期!!!!!
 回复 引用   
#15楼 2007-03-07 22:15 | Woodcode[未注册用户]
dflying一直出好文章, 我在这学到很多
 回复 引用   
#16楼[楼主] 2007-03-07 22:26 | Dflying Chen      
@Frank
@笑疯^_^
@Min.W
@xyun
@郑海恭
@Woodcode
谢谢支持,我马上写下篇

 回复 引用 查看   
#17楼[楼主] 2007-03-07 22:27 | Dflying Chen      
@xyun
跨框架是什么意思呢?

 回复 引用 查看   
#18楼 2007-03-08 09:17 | xyun[未注册用户]
@Dflying Chen
如果包含控件得页面放在一个frame中
控件常常不能显示完整。
我希望控件能浮在frame之上。

 回复 引用   
#19楼[楼主] 2007-03-08 16:18 | Dflying Chen      
@xyun
这是IE的Bug吧,在Panel中加上iframe就好了

 回复 引用 查看   
#20楼 2007-03-13 20:25 | tloner      
public string ServicePath
{
get
{
return GetPropertyValue<string>("ServicePath", string.Empty);
}
set
{
SetPropertyValue<string>("ServicePath", value);
}
}
客户端脚本中没有“ServicePath”,这里怎么能Get或Set呢?

客户端脚本中有:
get_ServicePath : function() {
return this._servicePath;
},

set_ServicePath : function(value) {
if (this._servicePath != value) {
this._servicePath = value;
}
},难道就对应这两个吗?它们的命名有什么规律吗

 回复 引用 查看   
#21楼[楼主] 2007-03-14 00:51 | Dflying Chen      
@tloner
命名规则就是,C#中写属性名称,到了JavaScript中要在前面加上get_和set_,不好意思在文中忘了说明

 回复 引用 查看   
#22楼 2007-03-14 14:36 | tloner      
呵呵,我想起来了,Ajax里是有这条规则
 回复 引用 查看   
#23楼[楼主] 2007-03-14 17:20 | Dflying Chen      
@tloner
恩,是阿,记得很久以前就是这个说法了

 回复 引用 查看   
除非特别声明,本站内所有资源,包括但不限于文章,代码,图片等,均应用于Dflying版权说明
关于ASP.NET AJAX,您可以:
直接阅读ASP.NET AJAX文章分类
Atlas文章打包下载(截至4/28/2006)
加入ASP.NET AJAX学习团队
询问关于ASP.NET AJAX的问题
加入ASP.NET AJAX讨论群
阅读愚作《ASP.NET AJAX程序设计》
点击阅读
点击阅读


关于Windows Vista,您可以:
加入Windows Vista开发团队!
昵称:Dflying Chen
园龄:5年10个月
粉丝:127
关注:0

搜索

 
 

最新随笔

随笔分类(352)

随笔档案(313)

Blog Roll

Dflying的其他Blog

Online Chat

统计信息

积分与排名

  • 积分 - 2442908
  • 排名 - 7

最新评论

阅读排行榜

评论排行榜