【问题标题】:Adding server-side event to extender control将服务器端事件添加到扩展器控件
【发布时间】:2008-09-01 07:04:07
【问题描述】:

我有一个扩展器控件,它会在用户完成输入 500 毫秒后引发文本框的 OnTextChanged 事件。这样做的问题是当文本框失去焦点时会引发OnTextChanged,这会导致问题(因为回发)。

我想做的是让扩展器控制它自己的服务器端事件(例如,OnDelayedSubmit),这样我就可以单独处理它了。该事件将源自扩展器控件的行为脚本(在 500 毫秒延迟之后),因此不能在 onchanged 中添加 __doPostBack

任何人都可以阐明如何解决这个问题吗?

【问题讨论】:

    标签: asp.net .net-3.5


    【解决方案1】:

    在大量阅读扩展器控件和 JavaScript 之后,我拼凑出一个目前看来可行的解决方案。

    主要技巧是从服务器端获取必要的回发代码到客户端行为脚本。我通过使用ExtenderControlProperty(在控件的OnPreRender 函数中设置)来做到这一点,然后在行为脚本中进行评估。剩下的就是基本的事件处理。

    所以现在我的扩展器控件的 .cs 文件看起来像这样:

    public class DelayedSubmitExtender : ExtenderControlBase, IPostBackEventHandler
    {
        // This is where we'll give the behavior script the necessary code for the 
        // postback event
        protected override void OnPreRender(EventArgs e)
        {
            string postback = Page.ClientScript.GetPostBackEventReference(this, "DelayedSubmit") + ";";
            PostBackEvent = postback;
        }
    
        // This property matches up with a pair of get & set functions in the behavior script
        [ExtenderControlProperty]
        public string PostBackEvent
        {
            get
            {
                return GetPropertyValue<string>("PostBackEvent", "");
            }
            set
            {
                SetPropertyValue<string>("PostBackEvent", value);
            }
        }
    
        // The event handling stuff
        public event EventHandler Submit;  // Our event
    
        protected void OnSubmit(EventArgs e)  // Called to raise the event
        {
            if (Submit != null)
            {
                Submit(this, e);
            }
        }
    
        public void RaisePostBackEvent(string eventArgument)  // From IPostBackEventHandler
        {
            if (eventArgument == "DelayedSubmit")
            {
                OnSubmit(new EventArgs());
            }
        }
    
    }
    

    我的行为脚本看起来像这样:

    DelayedSubmitBehavior = function(element) {
        DelayedSubmitBehavior.initializeBase(this, [element]);
    
        this._postBackEvent = null; // Stores the script required for the postback
    }
    
    DelayedSubmitBehavior.prototype = {
        // Delayed submit code removed for brevity, but normally this would be where 
        // initialize, dispose, and client-side event handlers would go
    
        // This is the client-side part of the PostBackEvent property
        get_PostBackEvent: function() {
            return this._postBackEvent;
        },
        set_PostBackEvent: function(value) {
            this._postBackEvent = value;
        }
    
        // This is the client-side event handler where the postback is initiated from
        _onTimerTick: function(sender, eventArgs) {
            // The following line evaluates the string var as javascript,
            // which will cause the desired postback
            eval(this._postBackEvent);
        }
    }
    

    现在可以像处理任何其他控件上的事件一样处理服务器端事件。

    【讨论】:

      猜你喜欢
      • 2011-10-09
      • 1970-01-01
      • 1970-01-01
      • 2015-08-08
      • 1970-01-01
      • 1970-01-01
      • 2020-09-01
      • 1970-01-01
      • 2022-01-18
      相关资源
      最近更新 更多