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: " " 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擴展
總結
以上是生活随笔為你收集整理的ASP.NET AJAX 控件开发基础的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ICE专题:ICE简介
- 下一篇: NHibernate for .NET