Pregunta

Tengo un control extensor que genera un cuadro de texto. OnTextChanged evento 500 ms después de que el usuario haya terminado de escribir.El problema con esto es que OnTextChanged aparece cuando el cuadro de texto pierde el foco, lo que causa problemas (debido a la devolución de datos).

Lo que me gustaría hacer es darle al control del extensor su propio evento del lado del servidor (digamos, OnDelayedSubmit) para poder manejarlo por separado.El evento se originará en el script de comportamiento del control extensor (después del retraso de 500 ms), por lo que poner un __doPostBack en onchanged no es una opinión.

¿Alguien puede arrojar luz sobre cómo hacer esto?

¿Fue útil?

Solución

Después de leer mucho sobre controles extensores y JavaScript, he creado una solución que parece estar funcionando hasta ahora.

El truco principal fue obtener el código de devolución necesario desde el lado del servidor al script de comportamiento del lado del cliente.Hice esto usando un ExtenderControlProperty (que se establece en el control OnPreRender función), y luego evaluado en el script de comportamiento.El resto fueron cosas básicas de manejo de eventos.

Así que ahora mi control extensor .cs El archivo se parece a esto:

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

}

Y mi script de comportamiento se parece a esto:

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

Ahora el evento del lado del servidor se puede manejar de la misma manera que manejaría un evento en cualquier otro control.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top