So, here's what I've found. There are two cases one might wish to disable a DirectEvent. One is in the case of OnInit; the other is in a DirectEvent.
In the case of an OnInit, I frequently will initialize a button (or other control) with both a Listener and a DirectEvent in the ascx file for the same event. Then during Initialization set the Clicks Listener's Handler to null or -= the Click DirectEvent's Event.
In the case of a DirectEvent, I need to be able to turn off Events and Listeners on the same or other controls.
Originally Posted by
Daniil
I think calling "-=" in OnInit event should work.
I've found this however does work in some cases, but not all. I haven't worked out why that is yet. However, I created a sample below that reliably does not.
This works in the OnInit though.
Originally Posted by
jwf
grid.DirectEvents.ItemDblClick.Before = "return false;";
However, all 3 scenarios fail in a DirectEvent. With Listeners, you can't set the Click Handler to NULL. You can't set Events -= or set it's Before. No code renders back.
In this sample of code, I create a Button with both DirectEvent & Listener for Click. During OnInit I try to disable the Event without effect just to show you. I remark out a piece of code that sets the Listeners handler to null but that works so it's insignificant here.
If you run the code, after the first time you click the button, it displays a standard javascript alert from the listener, then a Ext.Net Alert from the Direct Event. After the DirectEvent I'd hoped to turn it off so nothing happens if you click the button again, but both occur still.
<%@ Page Language="C#" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<script runat="server">
protected void Page_Load( object sender, EventArgs e ) {
if( X.IsAjaxRequest )
return;
Btn.DirectEvents.Click.Event -= OnClick; // This should have turned off the DirectEvent; it does not.
//Btn.Listeners.Click.Handler = null; // When remark is removed, this scenario at least works
}
public void OnClick( object sender, DirectEventArgs e ) {
Btn.DirectEvents.Click.Before = "return false;"; // This does nothing
Btn.Listeners.Click.Handler = null;// This does nothing
X.MessageBox.Alert( "Test", "Test" ).Show();
}
</script>
<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
<title>Test33</title>
</head>
<body>
<form id="Form1" runat="server">
<ext:ResourceManager ID="ResourceManager1" runat="server" ViewStateMode="Enabled" ScriptMode="Debug" SourceFormatting="true" />
<ext:Viewport ID="vp" runat="server">
<Items>
<ext:Button ID="Btn" runat="server" Text="Test">
<Listeners>
<Click Handler="alert('test');" />
</Listeners>
<DirectEvents>
<Click OnEvent="OnClick"></Click>
</DirectEvents>
</ext:Button>
</Items>
<Items>
</Items>
</ext:Viewport>
</form>
</body>
</html>
Ideally, I don't know what you guys think it should be doing here, but it REALLY would be nice if you guys added a method to disable/enable events to the trunk [A FEATURE REQUEST].
I tried adding this to the ObservableDirectEvent class just for testing, and yeah, it works for OnInit, but I think you guys can do better; one that should work in DirectEvents too.
public virtual void DisableEvent()
{
var before = this.Before;
if( !string.IsNullOrEmpty( before ) )
this.Before = "return false;;" + before;
else
this.Before = "return false;;";
}
public virtual void EnableEvent()
{
var before = this.Before;
if( !string.IsNullOrEmpty( before ) && before.StartsWith( "return false;;" ) )
this.Before = before.Substring( 14 );
}
}
Please help.