添加服务器端事件来扩展程序控件 [英] Adding server-side event to extender control

查看:110
本文介绍了添加服务器端事件来扩展程序控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展控件,提出了一个文本框的 OnTextChanged 500ms的事件后,用户完成输入。这样做的问题是, OnTextChanged 被提出时,文本框失去焦点,这将导致(因为回传的)问题。

I have an extender control that raises a textbox's OnTextChanged event 500ms after the user has finished typing. The problem with this is that OnTextChanged gets raised when the textbox loses focus, which causes problems (because of the postback).

我想要做的就是给扩展器控制自己的服务器端事件(例如, OnDelayedSubmit ),所以我可以单独处理。本次活动将在扩展控件的行为脚本来源(在500毫秒的延迟之后),所以把一个 __ doPostBack 调用onChanged 是不是一种选择。

What I'd like to do is give the extender control its own server-side event (say, OnDelayedSubmit) so I can handle it separately. The event will originate in the extender control's behavior script (after the 500ms delay), so putting a __doPostBack in onchanged is not an option.

任何人都可以阐明如何去呢?

Can anyone shed light on how to go about this?

推荐答案

大量的扩展程序控件和JavaScript看完了之后,我拼凑起来的解决方案,这似乎至今进行的工作。

After plenty of reading up on extender controls and JavaScript, I've cobbled together a solution that seems to be working so far.

主要的窍门是越来越从服务器端所需的回传code到客户端行为的脚本。我用这样做的 ExtenderControlProperty (这是在设置控件的在preRender 功能),然后eval'd的行为脚本。其余的是基本的事件处理的东西。

The main trick was getting the necessary postback code from server-side to the client-side behavior script. I did this by using an ExtenderControlProperty (which is set in the control's OnPreRender function), and then eval'd in the behavior script. The rest was basic event-handling stuff.

所以,现在我的扩展程序控件的的.cs 文件看起来是这样的:

So now my extender control's .cs file looks something like this:

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());
        }
    }

}

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

And my behavior script looks something like this:

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);
    }
}

现在的服务器端事件可以同样的方式处理你处理任何其他控件的事件。

Now the server-side event can be handled the same way you'd handle an event on any other control.

这篇关于添加服务器端事件来扩展程序控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆