ASP.NET AJAX 控件开发基础

在 JavaScript 当前广泛使用的版本中,它缺少 .NET 开发人员所熟悉的几个 OOP 的关键概念,而 ASP.NET AJAX 可以模拟其中的大多数,而且 ASP.NET AJAX 的目标是将使用 .NET 的开发人员所熟悉的某些其他构造(例如属性、事件、枚举和接口)转换成 JavaScript.ASP.NET AJAX 中的反射 API 将检查所有类型(无论是内置类型、类、接口、命名空间、或者甚至是枚举),而它们包括的类似 .NET Framework 的函数(例如 isInstanceOfType 和 inheritsFrom)可以在运行时检查类的层次结构。

下面是一个典型的AjaxControlToolkit的控件脚本,红色部分为添加的解释语句:

// (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.

Type.registerNamespace('AjaxControlToolkit');   //定义命名空间

//在 ASP.NET AJAX 中定义类,您需要将其构造函数赋给变量(注意,构造函数如何调用基础函数):

AjaxControlToolkit.ConfirmButtonBehavior = function(element) {
/// <summary>
/// The ConfirmButtonBehavior extends buttons by providing a confirmation dialog when clicked
/// </summary>
/// <param name="element" type="Sys.UI.DomElement" domElement="true">
/// Button the behavior is associated with
/// </param>

//调用初始化基类,类似于C++/C# base关键字
AjaxControlToolkit.ConfirmButtonBehavior.initializeBase(this, [element]);

// Confirm text
this._ConfirmTextValue = null;
// Click handler for the target control
this._clickHandler = null;
}

//通过prototype定义成员()
AjaxControlToolkit.ConfirmButtonBehavior.prototype = {

//初始化资源
initialize : function() {
/// <summary>
/// Initialize the behavior
/// </summary>
AjaxControlToolkit.ConfirmButtonBehavior.callBaseMethod(this, 'initialize');
// Attach the handler
this._clickHandler = Function.createDelegate(this, this._onClick);
$addHandler(this.get_element(), "click", this._clickHandler);
},

//释放资源

dispose : function() {
/// <summary>
/// Dispose the behavior
/// </summary>

// Detach event handlers
if (this._clickHandler) {
$removeHandler(this.get_element(), "click", this._clickHandler);
this._clickHandler = null;
}

AjaxControlToolkit.ConfirmButtonBehavior.callBaseMethod(this, 'dispose');
},

_onClick : function(e) {
/// <summary>
/// Button's click handler to display the confirmation dialog
/// </summary>
/// <param name="e" type="Sys.UI.DomEvent">
/// Event info
/// </param>

if (this.get_element() && !this.get_element().disabled) {
// Display confirm dialog and return result to allow cancellation
if (!window.confirm(this._ConfirmTextValue)) {
e.preventDefault();
return false;
}
}
},

get_ConfirmText : function() {
/// <value type="String">
/// The text to show when you want to confirm the click. (Note: HTML entities can be used here (ex: "&#10;" for new-line))
/// </value>
return this._ConfirmTextValue;
},
set_ConfirmText : function(value) {
if (this._ConfirmTextValue != value) {
this._ConfirmTextValue = value;
this.raisePropertyChanged('ConfirmText');
}
}
}

//最终注册类:
AjaxControlToolkit.ConfirmButtonBehavior.registerClass('AjaxControlToolkit.ConfirmButtonBehavior', AjaxControlToolkit.BehaviorBase);

 

参考:[ASP.NET AJAX]类似.NET框架的JavaScript扩展

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值