ASP.NET: Fixing a Bug in the MS AJAX Client-Side
Friday, May 2nd, 2008I discovered a problem with the ASP.NET AJAX Client-Side library. It seems it was trying to access a property on a variable when the variable was not defined. The difficult part of the problem was that the function that was being called is a part of a series of events that are bound to various elements on the page automatically. I am unaware of what it is doing exactly or how to prevent it so fixing it was a challenge. Fundamentally the function was working like this...
Because the a parameter was passed in as undefined from a function that my custom code did not call I could not simply fix it on my end. I needed a way to first check if the a value was defined before it attempts to access the disabled property on it. Since JavaScript is a dynamic language I can redefine and remap functions very easily. I dug into the JavaScript in the MS AJAX Client-Side library and found the context for the function and wrapped it with my own function that checks if the given parameter is defined. If the value is defined I call the original function.
The code below is my fix. The _onFormElementActive function which is defined in Sys.WebForms is the function that was causing me trouble. This code first saves the existing function to a local reference and then reassigns a new function to that name within the prototype. Then within that function I name the function with FIXED at the end the name so that it can be called within the context of the object instance. The original function needed to access this with the right context which is why that is necessary. Then I can simply check the value of a and call the fixed function with the same parameters.
The beauty of this code and this solution is how JavaScript works. I can safely redefine an existing function, wrapping it with my own code, and still call the previous function easily. And even if the object instance for this class was already defined this adjustment will still take place because this change not only affects all instances created in the future, but all pre-existing instances.
Next I hope to get in touch with the ASP.NET AJAX team and let them know about this particular problem. Ultimately they may just need to code to read if (a && a.disabled) and the bug would be corrected. I also want to know how this problem came up. This web site was working fine for a couple of weeks before this started to happen. And curiously enough, it only affects IE7 and not IE6 or FF2.

