Po wielu lekturach na temat kontroli ekstenderów i JavaScriptu, stworzyłem rozwiązanie, które jak na razie działa.
Główna sztuczka polegała na uzyskaniu niezbędnego kodu zwrotnego od strony serwera do skryptu zachowania po stronie klienta. Zrobiłem to za pomocą ExtenderControlProperty
(który jest ustawiony w funkcji kontrolnej OnPreRender
), a następnie eval'd w skrypcie zachowania. Reszta była podstawową sprawą obsługi zdarzeń.
Więc teraz moje Extender kontrolki .cs
plik wygląda mniej więcej tak:
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());
}
}
}
A mój skrypt zachowanie wygląda mniej więcej tak:
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);
}
}
Teraz zdarzenie po stronie serwera może być obsługiwane w ten sam sposób, w jaki poradzisz sobie z jakimkolwiek innym elementem kontrolnym.