Ajax,Callback,postback和Sys.WebForms.PageRequestManager.getInstance()。add_beginRequest

时间:2010-05-11 22:49:58

标签: c# asp.net ajax callback webforms

我有一个用户控件,它封装了一个NumericUpDownExtender。此UserControl实现接口ICallbackEventHandler,因为当用户更改与要在服务器中引发的自定义事件关联的文本框的值时,我希望这样做。另一方面,每次完成异步回发时,我都会发送加载和禁用整个屏幕的消息。当通过以下代码行更改某些内容(例如UpdatePanel)时,这非常有效:

Sys.WebForms.PageRequestManager.getInstance()。add_beginRequest(     function(sender,args){          var modalPopupBehavior = $ find('programmaticSavingLoadingModalPopupBehavior');         modalPopupBehavior.show();     } );

UserControl放置在详细信息视图中,该视图位于aspx中的UpdatePanel内。当引发自定义事件时,我希望aspx中的另一个文本框更改其值。到目前为止,当我单击UpDownExtender时,它会正确地进入服务器并引发自定义事件,并在服务器中分配文本框的新值。但它在浏览器中没有改变。

我怀疑问题是回调,因为我有一个带有AutoCompleteExtender的UserControl的相同架构,它实现了IPostbackEventHandler并且它可以工作。 任何线索我如何解决这个问题,使UpDownNumericExtender用户控件像AutComplete一样工作?

这是用户控件的代码和父代:

using System;
using System.Web.UI;
using System.ComponentModel;
using System.Text;

namespace Corp.UserControls
{
    [Themeable(true)]
    public partial class CustomNumericUpDown : CorpNumericUpDown, ICallbackEventHandler
    {
        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                currentInstanceNumber = CorpAjaxControlToolkitUserControl.getNextInstanceNumber();
            }
            registerControl(this.HFNumericUpDown.ClientID, currentInstanceNumber);
            string strCallServer = "NumericUpDownCallServer" + currentInstanceNumber.ToString();

            // If this function is not written the callback to get the disponibilidadCliente doesn't work
            if (!Page.ClientScript.IsClientScriptBlockRegistered("ReceiveServerDataNumericUpDown"))
            {
                StringBuilder str = new StringBuilder();
                str.Append("function ReceiveServerDataNumericUpDown(arg, context) {}").AppendLine();
                Page.ClientScript.RegisterClientScriptBlock(typeof(CorpNumericUpDown),
                                                            "ReceiveServerDataNumericUpDown", str.ToString(), true);
            }

            nudeNumericUpDownExtender.BehaviorID = "NumericUpDownEx" + currentInstanceNumber.ToString();
            ClientScriptManager cm = Page.ClientScript;
            String cbReference = cm.GetCallbackEventReference(this, "arg", "ReceiveServerDataNumericUpDown", "");
            String callbackScript = "function " + strCallServer + "(arg, context)" + Environment.NewLine + "{" + Environment.NewLine + cbReference + ";" + Environment.NewLine + "}" + Environment.NewLine;
            cm.RegisterClientScriptBlock(typeof(CustomNumericUpDown), strCallServer, callbackScript, true);
            base.Page_PreRender(sender,e);
        }

        [System.ComponentModel.Browsable(true)]
        [System.ComponentModel.Bindable(true)]
        public Int64 Value
        {
            get { return (string.IsNullOrEmpty(HFNumericUpDown.Value) ? Int64.Parse("1") : Int64.Parse(HFNumericUpDown.Value)); }
            set
            {
                HFNumericUpDown.Value = value.ToString();
                //txtAutoCompleteCliente_AutoCompleteExtender.ContextKey = value.ToString();
                // TODO: Change the text of the textbox
            }
        }

        [System.ComponentModel.Browsable(true)]
        [System.ComponentModel.Bindable(true)]
        [Description("The text of the numeric up down")]
        public string Text
        {
            get { return txtNumericUpDown.Text; }
            set { txtNumericUpDown.Text = value; }
        }

        public delegate void NumericUpDownChangedHandler(object sender, NumericUpDownChangedArgs e);
        public event NumericUpDownChangedHandler numericUpDownEvent;

        [System.ComponentModel.Browsable(true)]
        [System.ComponentModel.Bindable(true)]
        [System.ComponentModel.Description("Raised after the number has been increased or decreased")]
        protected virtual void OnNumericUpDownEvent(object sender, NumericUpDownChangedArgs e)
        {
            if (numericUpDownEvent != null) //check to see if anyone has attached to the event 
                numericUpDownEvent(this, e);
        }

        #region ICallbackEventHandler Members

        public string GetCallbackResult()
        {
            return "";//throw new NotImplementedException();
        }

        public void RaiseCallbackEvent(string eventArgument)
        {
            NumericUpDownChangedArgs nudca = new NumericUpDownChangedArgs(long.Parse(eventArgument));
            OnNumericUpDownEvent(this, nudca);
        }

        #endregion
    }

    /// <summary>
    /// Class that adds the prestamoList to the event
    /// </summary>
    public class NumericUpDownChangedArgs : System.EventArgs
    {
        /// <summary>
        /// The current selected value.
        /// </summary>
        public long Value { get; private set; }

        public NumericUpDownChangedArgs(long value)
        {
            Value = value;
        }
    }

}


using System;
using System.Collections.Generic;
using System.Text;

namespace Corp
{
    /// <summary>
    /// Summary description for CorpAjaxControlToolkitUserControl
    /// </summary>
    public class CorpNumericUpDown : CorpAjaxControlToolkitUserControl
    {
        private Int16 _currentInstanceNumber; // This variable hold the instanceNumber assignated at first place.

        public short currentInstanceNumber
        {
            get { return _currentInstanceNumber; }
            set { _currentInstanceNumber = value; }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            const string strOnChange = "OnChange";
            const string strCallServer = "NumericUpDownCallServer";

            StringBuilder str = new StringBuilder();
            foreach (KeyValuePair<String, Int16> control in controlsToRegister)
            {
                str.Append("function ").Append(strOnChange + control.Value).Append("(sender, eventArgs) ").AppendLine();
                str.Append("{").AppendLine();
                str.Append("    if (sender) {").AppendLine();
                str.Append("        var hfield = document.getElementById('").Append(control.Key).Append("');").AppendLine();
                str.Append("        if (hfield.value != eventArgs) {").AppendLine();
                str.Append("           hfield.value = eventArgs;").AppendLine();
                str.Append("           ").Append(strCallServer + control.Value).Append("(eventArgs, eventArgs);").AppendLine();
                str.Append("        }").AppendLine();
                str.Append("    }").AppendLine();
                str.Append("}").AppendLine();
                Page.ClientScript.RegisterClientScriptBlock(typeof(CorpNumericUpDown), Guid.NewGuid().ToString(), str.ToString(), true);
            }

            str = new StringBuilder();
            foreach (KeyValuePair<String, Int16> control in controlsToRegister)
            {
                str.Append("    funcsPageLoad[funcsPageLoad.length] = function() { $find('NumericUpDownEx" + control.Value + "').add_currentChanged(").Append(strOnChange + control.Value).Append(");};").AppendLine();
                str.Append("    funcsPageUnLoad[funcsPageUnLoad.length] = function() { $find('NumericUpDownEx" + control.Value + "').remove_currentChanged(").Append(strOnChange + control.Value).Append(");};").AppendLine();
            }
            Page.ClientScript.RegisterClientScriptBlock(typeof(CorpNumericUpDown), Guid.NewGuid().ToString(), str.ToString(), true);
        }
    }
}

并创建加载视图我使用它:

//在异步回发处理开始之前引发beginRequest事件,并将回发发送到服务器。您可以使用此事件来调用自定义脚本来设置请求标头或启动动画,以通知用户正在处理回发。 Sys.WebForms.PageRequestManager.getInstance()。add_beginRequest(     function(sender,args){          var modalPopupBehavior = $ find('programmaticSavingLoadingModalPopupBehavior');         modalPopupBehavior.show();     } );

//在异步回发完成并且控件已返回浏览器后引发endRequest事件。您可以使用此事件向用户提供通知或记录错误。 Sys.WebForms.PageRequestManager.getInstance()。add_endRequest(     function(sender,arg){         var modalPopupBehavior = $ find('programmaticSavingLoadingModalPopupBehavior');         modalPopupBehavior.hide();     } );

提前致谢!丹尼尔。

1 个答案:

答案 0 :(得分:2)

我假设你从五月开始就想到了这一点,或者采取了另一条路来解决这个问题,但是会对可能遇到同样问题的其他用户做出回应。

我的理解是,在进行webform回调之后发送回客户端浏览器的唯一响应是GetCallbackResult返回的字符串。然后创建一个名为与传递给Page.ClientScript.GetCallbackEventReference的clientCallback参数相同的javascript方法,在您的情况下为“ReceiveServerDataNumericUpDown”。

然后,此函数可以更新客户端文本框的值。