Friday, February 18, 2005
ASP.NET Visual Studio and Dissapearing events
If you use Visual Studio 2003 to development code behind ASP.NET pages, you have probably stumbled upon a bug, in which the events on some server controls simply disappear. I have talked with Microsoft about this and they have confirmed this to be a bug but don't have a fix for this simply a work around. What's happening is when switching between design and html view the ide changes the contents of the InitializeComponent() function which holds all of the delegates for the events of your controls. This is normal behaviour, the bug comes in when every now and then and for some undetectable reason the entire contents of this function get erased, deleting all of your delegates. Microsoft's work around is to not use the IDE to wire up the events but instead to add the specific code the server controls tag. For example <ASP:TextBox ID=Textbox1 OnClick="MyEventHandler()" RUNAT="Server" > This works, if you are working with noobs to ASP.NET they only live and breath in the IDE and rarely look at the underlying tags and stay in design mode almost exclusively. When these noobs try to figure how or why the click event still fires even though there isn't a delegate, they typically add a delegate causing the event to fire twice. This basically hides functionality. My work around is to simply take the generated code out of initializeComponent() function and move into the OnInit() function, this function does not appear to be touched by the IDE and the code never seems to thrashed. And it makes the delegation code plainly visible to all the noobs out there, who might maintain your code at a later date.