首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ajax、回调、回发和Sys.WebForms.PageRequestManager.getInstance().add_beginRequest

Ajax、回调、回发和Sys.WebForms.PageRequestManager.getInstance().add_beginRequest
EN

Stack Overflow用户
提问于 2010-05-12 06:49:58
回答 1查看 7.6K关注 0票数 1

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

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

UserControl被放在aspx中的UpdatePanel内部的detailsview视图中。当自定义事件被引发时,我希望aspx中的另一个文本框更改它的值。到目前为止,当我单击UpDownExtender时,它会正确地转到服务器并引发自定义事件,并在服务器中分配textbox的新值。但它不会在浏览器中更改。

我怀疑问题出在回调上,因为我对一个实现了IPostbackEventHandler的UserControl和一个实现了IPostbackEventHandler的AutoCompleteExtender使用了相同的体系结构,并且它可以工作。我该如何解决这个问题,让UpDownNumericExtender用户控件像AutComplete用户控件一样工作呢?

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

代码语言:javascript
复制
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(函数( $find('programmaticSavingLoadingModalPopupBehavior');,args) { var modalPopupBehavior =var modalPopupBehavior.show();} );

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

提前感谢!丹尼尔。

EN

回答 1

Stack Overflow用户

发布于 2010-08-14 00:21:10

我假设你从五月份就知道了这一点,或者采取了另一种方法来解决这个问题,但如果其他用户遇到同样的问题,你会做出回应。

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

然后,此函数可以在客户端执行更新textbox的值的工作。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2814974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档