Pergunta

Eu tenho um controle extensor que gera uma caixa de texto OnTextChanged evento 500 ms após o usuário terminar de digitar.O problema com isso é que OnTextChanged é gerado quando a caixa de texto perde o foco, o que causa problemas (por causa do postback).

O que eu gostaria de fazer é dar ao extensor o controle de seu próprio evento do lado do servidor (digamos, OnDelayedSubmit) para que eu possa lidar com isso separadamente.O evento terá origem no script de comportamento do controle extensor (após o atraso de 500ms), portanto, colocar um __doPostBack em onchanged não é uma opção.

Alguém pode esclarecer como fazer isso?

Foi útil?

Solução

Depois de ler bastante sobre controles extensores e JavaScript, criei uma solução que parece estar funcionando até agora.

O truque principal era obter o código de postback necessário do lado do servidor para o script de comportamento do lado do cliente.Eu fiz isso usando um ExtenderControlProperty (que é definido no controle OnPreRender função) e depois avaliado no script de comportamento.O resto eram coisas básicas de manipulação de eventos.

Então agora meu controle extensor está .cs arquivo se parece com isto:

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

}

E meu script de comportamento é mais ou menos assim:

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

Agora o evento do lado do servidor pode ser tratado da mesma forma que você trataria um evento em qualquer outro controle.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top