使用ASP.NET AJAX刷新没有更新程序面板的控件

时间:2008-11-19 03:51:14

标签: asp.net ajax

此问题与我之前的问题ASP.NET AJAX

直接相关

是否可以异步执行回发?我在页面上有多个CustomTextbox控件,当我回发时我想更新表单上的另一个控件。

如果我在第一篇文章之后将所有控件放回更新程序面板中,则需要几秒钟才能完成,我对其他控件进行的任何更改都会以原始值进行渲染。

知道如何解决这个问题吗?

Type.registerNamespace('Demo');

Demo.CustomTextBox = function(element) {
    Demo.CustomTextBox.initializeBase(this, [element]);
}

Demo.CustomTextBox.prototype = {

    initialize: function() {
        Demo.CustomTextBox.callBaseMethod(this, 'initialize');

        this._onblurHandler = Function.createDelegate(this, this._onBlur);

        $addHandlers(this.get_element(),
                     {
                         'blur': this._onBlur
                     },
                     this);
    },

    dispose: function() {
        $clearHandlers(this.get_element());

        Demo.CustomTextBox.callBaseMethod(this, 'dispose');
    },

    _onBlur: function(e) {
    if (this.get_element() && !this.get_element().disabled) {
            /* Cridit to AdamB for this line of code */
            __doPostBack(this.get_element().id, 0);
        }
    }
}

Demo.CustomTextBox.registerClass('Demo.CustomTextBox', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

1 个答案:

答案 0 :(得分:2)

只将更新面板放在需要更新的控件周围,并在该更新面板上设置触发更改触发器的控件:

<asp:TextBox runat="server" ID="Entry2" />
<asp:TextBox runat="server" ID="Entry1" />
<asp:UpdatePanel>
    <ContentTemplate>
        <asp:TextBox runat="server" ID="Result" ReadOnly="true" />
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Entry1" />
        <asp:AsyncPostBackTrigger ControlID="Entry2" />
    </Triggers>
</asp:UpdatePanel>

因此,当回发完成时,只有更新面板内的内容会发生变化。 AJAX回发页面上所有控件的值,而不仅仅是内容模板中的值。

选项2(以及更多参与)将编写一个执行计算的Web服务和一些javascript来调用它。