【问题标题】:Ajax, Callback, postback and Sys.WebForms.PageRequestManager.getInstance().add_beginRequestAjax、回调、回发和 Sys.WebForms.PageRequestManager.getInstance().add_beginRequest
【发布时间】:2011-02-18 09:19:35
【问题描述】:

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

Sys.WebForms.PageRequestManager.getInstance().add_beginRequest( 功能(发件人,参数){ var modalPopupBehavior = $find('programmaticSavingLoadingModalPopupBehavior'); modalPopupBehavior.show(); } );

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

我怀疑问题出在回调上,因为我的 UserControl 与 AutoCompleteExtender 具有相同的架构,它实现了 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( 功能(发件人,参数){ var modalPopupBehavior = $find('programmaticSavingLoadingModalPopupBehavior'); modalPopupBehavior.show(); } );

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

提前致谢!丹尼尔。

【问题讨论】:

    标签: c# asp.net ajax callback webforms


    【解决方案1】:

    我假设您从 5 月开始就解决了这个问题,或者采取了另一种方法来解决问题,但会回复可能遇到相同问题的其他用户。

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

    然后这个函数可以在客户端更新文本框的值。

    【讨论】:

      猜你喜欢
      • 2020-02-07
      • 1970-01-01
      • 2020-06-24
      • 1970-01-01
      • 2011-01-12
      • 2015-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多