如何将元素附加到另一个Ajax组件

时间:2013-06-13 14:10:00

标签: asp.net ajax asp.net-ajax

全部,原谅我,我不熟悉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检索组件,然后进行分离并附加工作。希望有人能给我一些帮助。谢谢。

1 个答案:

答案 0 :(得分:1)

经过一些研究后,我发现它在Asp.net Ajax中被称为Extend Web server control that encapsulates a client behavior,我发现组件的附件是由Asp.net自动完成的。我们可以看到Asp.net自动在aspx页面中生成Sys.Application.add_init(function()代码。因此,如果我们想要自定义Web服务器控件的原始行为,我相信它可以使用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对象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;
    }
};

请确保浏览器在原始代码之后解析此代码 我不确定这是否是正确的方法。我希望有人可以帮助验证它。非常感谢你。

这是tutorial。请检讨一下。 欢呼声。

相关问题