您的位置:首页 > 编程语言 > ASP

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

2012-01-15 04:07 866 查看
======================================================

注:本文源代码点此下载

======================================================

客户端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 getpropertyvaluestring>("servicepath", string.empty);
}
set
{
setpropertyvaluestring>("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
{
setpropertyvaluefloat>("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
{
}
}
到此为止,整个extender的编写就大功告成了!
源代码下载 http://files.cnblogs.com/dflying/popupnotificationextender_source.zip 接下来一篇讲讲使用方法以及一些深入的、实现原理之类的东西。

======================================================

在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定
这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐