Friday, May 9, 2008

Page_Load called twice

I hate it when the IDE makes code changes that you aren't aware of that could have negative repercussions that you aren't aware of until after deployment to production. I recently upgraded an app from .Net 1.1 to .Net 2.0 and everything seemed fine. Testing showed my calculations were unaffected by the upgrade.

Somehow since then, one or more of my codebehind pages had an additional event added to the Page_Load method. The Handles clause was changed to "Handles Mybase.Load, Me.Load". This had the unfortunate side effect of calling the method twice. One of the methods called in the Page_load method summed some data, but the private member variable where the calculation was stored was initialized to zero outside of the Page_Load method. This caused the sum to be 2X the actual value because the data was summed twice.

When and how was this extra event added? I don't know, but I will be on the lookout for it in the future as it can cause unexpected behavior at the most inopportune time.

No comments: