How to remove all event handlers from an event
I found a solution on the MSDN forums. The sample code below will remove all Click
events from button1
.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.Click += button1_Click;
button1.Click += button1_Click2;
button2.Click += button2_Click;
}
private void button1_Click(object sender, EventArgs e) => MessageBox.Show("Hello");
private void button1_Click2(object sender, EventArgs e) => MessageBox.Show("World");
private void button2_Click(object sender, EventArgs e) => RemoveClickEvent(button1);
private void RemoveClickEvent(Button b)
{
FieldInfo f1 = typeof(Control).GetField("EventClick",
BindingFlags.Static | BindingFlags.NonPublic);
object obj = f1.GetValue(b);
PropertyInfo pi = b.GetType().GetProperty("Events",
BindingFlags.NonPublic | BindingFlags.Instance);
EventHandlerList list = (EventHandlerList)pi.GetValue(b, null);
list.RemoveHandler(obj, list[obj]);
}
}
Best way to remove an event handler in jQuery?
jQuery ≥ 1.7
With jQuery 1.7 onward the event API has been updated, .bind()
/.unbind()
are still available for backwards compatibility, but the preferred method is using the on()/off() functions. The below would now be,
$('#myimage').click(function() { return false; }); // Adds another click event
$('#myimage').off('click');
$('#myimage').on('click.mynamespace', function() { /* Do stuff */ });
$('#myimage').off('click.mynamespace');
jQuery < 1.7
In your example code you are simply adding another click event to the image, not overriding the previous one:
$('#myimage').click(function() { return false; }); // Adds another click event
Both click events will then get fired.
As people have said you can use unbind to remove all click events:
$('#myimage').unbind('click');
If you want to add a single event and then remove it (without removing any others that might have been added) then you can use event namespacing:
$('#myimage').bind('click.mynamespace', function() { /* Do stuff */ });
and to remove just your event:
$('#myimage').unbind('click.mynamespace');
How to remove all eventhandler
Simply set the event to null
:
this.Something = null;
It will unregister all event handlers.
jQuery removing elements with event handlers
Event handlers, connected directly to DOM elements, die when the DOM elements are removed from the DOM.
Replacing the content is enough.
Delegated events as a better alternative:
The rules for deferred events (event delegation) are different, as the events are not actually connected to the individual DOM elements, but are captured at a higher level (like the document
). A selector is then run and the event function run against the matching element(s). Deferred events tie up less memory but are a tiny(-tiny) bit slower to run (you will never notice the difference for mouse events) as the element search is only done when the event triggers.
I would generally recommend using deferred on
, instead of connecting to lots of individual elements, especially when your DOM elements are loaded dynamically.
e.g.
$(document).on('someeventname', 'somejQueryselector', function(e){
// Handle the event here
});
Sequence of events with a delegated handler
- Attach a delegated handler to a single non-changing ancestor element (
document
is the best default for several reasons if nothing else is closer/convenient). See notes for details. - The chosen event bubbles up to the delegated target element
- The jQuery selector is applied to just the elements in the bubble-chain
- The event handler is run against only the matching elements that caused the event.
The upshot of this is that delegated handlers only match at event time, so can cope with dynamically added/removed content. The runtime overhead is actually lower at event registration (as it only connects to a single element) and the speed difference at event time is negligible (unless you can click a mouse 50,000 times per second!).
Notes:
- With delegated events, you should try to attach them to a single element, that is an ancestor of the target elements and that will not change.
- The fallbacks are usually
body
ordocument
if nothing else is handy. - Using
body
for delegated events can cause a bug to do with styling. This can mean mouse events do not bubble to body (if the computed height ofbody
is 0).document
is safer as it is not affected by styling. - Also
document
always exists, so you can attached delegated event handlers outside of a DOM-ready handler with no problems.
Should I remove event handlers from element before removeChild?
Removing the element from the DOM doesn't (or shouldn't) remove any of its listeners; after all, you may very well just be in the process of re-arranging your DOM elements, and as such you don't want to discard any listeners.
You can remove the listeners before, or after, it shouldn't make any difference.
If your plan is to remove the elements, and no longer use them, it would be wise to proceed with removing the events so as to avoid any possibility of memory leaks.
Does Javascript remove event handlers of deleted DOM elements?
Yes, modern browsers (eventually) release the memory used by event handlers in DOM nodes. However, old versions of Internet Explorer don't, so it's always good practice to remove event listeners before removing the nodes from the DOM.
This is a good article for understanding what's going on: http://msdn.microsoft.com/en-us/library/bb250448(v=vs.85).aspx
remove all EventHandlers of a specific Control
According to this,
for cancelling all Click Events of panel1 do this:
FieldInfo f1 = typeof(Control).GetField("EventClick", BindingFlags.Static| BindingFlags.NonPublic);
object obj = f1.GetValue(panel1);
PropertyInfo pi = panel1.GetType().GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Instance);
EventHandlerList list = (EventHandlerList)pi.GetValue(panel1, null);
list.RemoveHandler(obj, list[obj]);
And for cancelling other events of panel1 change EventClick
to event name that you want to remove.
One can get all event names using this code:
EventInfo[] info = type.GetEvents();
for (int i = 0; i < info.Length; i++)
{
Console.WriteLine(info[i].Name + "\n");
}
What's the good reason for removing an event after using it in javascript or unbinding after binding it in jquery?
The uses for this can be many and truly depends on your use case. Sometimes you only want an event to run once and you unbind after running your event listener finishes its function.
In certain apps such as a backbones app it comes down to performance. For example events that are bound to a view let's say, a view for an about page, will be persistant.
Let's say you want to switch between your about view to your "home" view. In backbone the events you define will still exist in memory even if you destroy the object they are bound to within the Dom. In the this case you will have a remove method, or something similar, that will unbind those events for your view object. This is extremely useful for a complex app. But these are only two examples. It's merely a tool and with that you can provide functionality for a large plethora of cases.
Related Topics
How Is the Boxing/Unboxing Behavior of Nullable<T> Possible
Generating a Random Decimal in C#
Exceptions That Can't Be Caught by Try-Catch Block in Application Code
Selenium: Drag and Drop from File System to Webdriver
Entity Framework 4: How to Find the Primary Key
C# Code to Linkify Urls in a String
Http Error 500.19 When Publish .Net Core Project into Iis with 0X80070005
How to Create an Instance from a String in C#
Why Does Guid.Tobytearray() Order the Bytes the Way It Does
How to Use Class Name as Parameter in C#
How to Detect Which .Net Runtime Is Being Used (Ms VS. Mono)
Simulating Cross Context Joins--Linq/C#
Difference Between the Keydown Event, Keypress Event and Keyup Event in Visual Studio