Damian Mehers' Blog Xamarin from Geneva, Switzerland.

13Aug/076

Dealing with "Sys.InvalidOperationException: Handler was not added through the Sys.UI.DomEvent.addHandler method."

In the ListSearch extender I recently had a bug where you got an exception when leaving a page that had a ListBox on it, where the ListBox was the target of both a CascadingDropDown extender and a ListSearch extender.

The reason was pretty dumb-- I was calling $clearHandlers(listBox) in the dispose method on the ListSearch Extender, and this was removing the handlers that the CascadingDropDown had set up.  So when the CascadingDropDown's dispose method tried to clean up its event subscriptions, the ASP.NET AJAX runtime complained that it was not subscribed.

The solution was replace calls to $addHandlers and $clearHandlers with individual calls to $addHandler and $removeHandler for each individual event in the ListSearch's initialize and dispose methods.

Unfortunately after implementing this solution I still got the same error ... after a lot of head-scratching, it turned out to be a simple typo.  I was calling $addHandler with 'keydown' as the event name, and calling $removeHandler with 'keyDown' as the event name.

A couple of things for you to check if you get the same error.

Filed under: AJAX 6 Comments