Domanda

Ho un controllo extender che genera una casella di testo OnTextChanged evento 500 ms dopo che l'utente ha finito di digitare.Il problema è questo OnTextChanged viene generato quando la casella di testo perde il focus, il che causa problemi (a causa del postback).

Quello che mi piacerebbe fare è dare all'extender il controllo del proprio evento lato server (ad esempio, OnDelayedSubmit) così posso gestirlo separatamente.L'evento avrà origine nello script di comportamento del controllo dell'extender (dopo il ritardo di 500 ms), quindi inserendo a __doPostBack In onchanged non è un'opzione.

Qualcuno può far luce su come procedere?

È stato utile?

Soluzione

Dopo aver letto molto sui controlli dell'extender e su JavaScript, ho messo insieme una soluzione che sembra funzionare finora.

Il trucco principale era ottenere il codice postback necessario dallo script di comportamento lato server allo script lato client.L'ho fatto usando un ExtenderControlProperty (che è impostato in control's OnPreRender function), e poi valutato nello script di comportamento.Il resto era roba di base per la gestione degli eventi.

Quindi ora il controllo del mio extender è .cs il file assomiglia a questo:

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 il mio script di comportamento assomiglia a questo:

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

Ora l'evento lato server può essere gestito nello stesso modo in cui gestiresti un evento su qualsiasi altro controllo.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top