[英]How to attach element to another Ajax component
全部,請原諒我對ASP.NET Ajax不熟悉。 我知道Create方法將html元素附加到ajax組件。 但是我不知道如何將其與當前組件分離。 並附上另一個。
假設有一個元素ctl00_PlaceHolderMain_UserRegistration_txbPassword1
已附加到組件類型AccelaWebControlExtender.HelperBehavior
,並且創建的組件ID為ctl00_PlaceHolderMain_UserRegistration_txbPassword1_helper_bhv
。 代碼如下所示。 請檢查一下。
Sys.Application.add_init(function() {
$create(AccelaWebControlExtender.HelperBehavior, {"closeTitle":"Close","id":"ctl00_PlaceHolderMain_UserRegistration_txbPassword1_helper_bhv","isRTL":false,"title":"Help"}, null, null, $get("ctl00_PlaceHolderMain_UserRegistration_txbPassword1"));
});
我認為首先應該通過id檢索組件,然后執行分離和附加工作。 希望有人能給我一些幫助。
經過研究,我發現它被稱為Extend Web server control that encapsulates a client behavior
在Asp.net Ajax中,並且我發現組件的附件是由Asp.net自動完成的。 我們可以看到Sys.Application.add_init(function()
代碼是由Asp.net自動在aspx頁面中生成的,因此,如果我們要自定義Web Server Control的原始行為,我相信可以在Javascript OOP中完成方式(舊的和相同的)。
例如:如果原始行為代碼是打擊。
// Register the namespace for the control.
Type.registerNamespace('Samples');
//
// Define the behavior properties.
//
Samples.FocusBehavior = function(element) {
Samples.FocusBehavior.initializeBase(this, [element]);
this._highlightCssClass = null;
this._nohighlightCssClass = null;
}
//
// Create the prototype for the behavior.
//
Samples.FocusBehavior.prototype = {
initialize : function() {
Samples.FocusBehavior.callBaseMethod(this, 'initialize');
$addHandlers(this.get_element(),
{ 'focus' : this._onFocus,
'blur' : this._onBlur },
this);
this.get_element().className = this._nohighlightCssClass;
},
dispose : function() {
$clearHandlers(this.get_element());
Samples.FocusBehavior.callBaseMethod(this, 'dispose');
},
//
// Event delegates
//
_onFocus : function(e) {
if (this.get_element() && !this.get_element().disabled) {
this.get_element().className = this._highlightCssClass;
}
},
_onBlur : function(e) {
if (this.get_element() && !this.get_element().disabled) {
this.get_element().className = this._nohighlightCssClass;
}
},
//
// Behavior properties
//
get_highlightCssClass : function() {
return this._highlightCssClass;
},
set_highlightCssClass : function(value) {
if (this._highlightCssClass !== value) {
this._highlightCssClass = value;
this.raisePropertyChanged('highlightCssClass');
}
},
get_nohighlightCssClass : function() {
return this._nohighlightCssClass;
},
set_nohighlightCssClass : function(value) {
if (this._nohighlightCssClass !== value) {
this._nohighlightCssClass = value;
this.raisePropertyChanged('nohighlightCssClass');
}
}
}
// Optional descriptor for JSON serialization.
Samples.FocusBehavior.descriptor = {
properties: [ {name: 'highlightCssClass', type: String},
{name: 'nohighlightCssClass', type: String} ]
}
// Register the class as a type that inherits from Sys.UI.Control.
Samples.FocusBehavior.registerClass('Samples.FocusBehavior', Sys.UI.Behavior);
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
我認為我們可以重寫Javascript Object Samples.FocusBehavior
及其某些原型對象的方法來實現自定義。
例如 。
我可以像這樣在腳本中覆蓋Samples.FocusBehavior.prototype._onFocus
。
Samples.FocusBehavior.prototype._onFocus = function (e) {
alert('test');
if (this.get_element() && !this.get_element().disabled) {
this.get_element().className = this._highlightCssClass;
}
};
只要確保此代碼在原始代碼之后被瀏覽器解析即可。
我不確定這是否是正確的方法。 希望有人可以幫助您進行驗證。非常感謝。
這是一個教程 。 請檢查一下。 干杯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.