Ajarn Mark Caldwell Blog

Bringing Business Sense to the IT World…

CodeBehind vs. CodeFile and the 'does not exist in the current context' Error

I find myself spending more time in .NET code lately.  While I was verifying my understanding of one of the other attributes of the @Page directive under the ASP.NET 2.0 (Visual Studio 2005) release, I stumbled across the following note regarding the CodeBehind attribute:

This attribute is included for compatibility with previous versions of ASP.NET, to implement the code-behind feature. In ASP.NET version 2.0, you should instead use the CodeFile attribute to specify the name of the source file, along with the Inherits attribute to specify the fully qualified name of the class.

So, I thought I would go ahead and clean up this little bit of deprecated code while I was working on this page.  It sounded like a good idea at the time, and everything ran fine for a while...but a while later, "all of a sudden" I started getting compiler errors saying that "The name 'myCheckBox' does not exist in the current context".  What made this really strange was that I had selected it from the IntelliSense selection list, which is usually very good at only providing names of objects that are valid.  And, on top of that, objects in the .aspx file should pretty much always be available and valid.  In fact, I was not getting errors on any other objects on the page that were being referenced in the routine.

So what happened?  Well, what I found was that my checkbox, and several other controls I had added recently, were not referenced in the .designer.cs file.  Therefore, when being compiled, the code-behind file didn't really know about the checkbox.  It is interesting that IntelliSense was able to recognize it, but the compiler did not.  The real culprit took some effort to find, because quite a bit of real-world time had elapsed between the change which caused the problem, and the change that discovered the problem.

It turns out that simply switching "CodeFile" in the Page directive back to "CodeBehind" and saving it, was enough to trigger the automatic update to the .designer.cs file and it caught up on all objects that were missing.  And suddenly everything compiled fine again.  I don't know why using "CodeFile" caused the automatic updating to suddenly stop, but this appears to be related to the fact that we are using a Web Application Project and not a Web Site.  While writing this blog post, I ran across this post by Steve Smith which appears to confirm that.

So, among the explicit lesson learned above, it also is a good reminder that there is some wisdom in the old saying, "if it ain't broke, don't fix it".  I'm generally in favor of refactoring code, but this was one case where I just threw it in along with a bunch of other changes I was making, and it bit me.  Thankfully the problem came up quickly enough that I remembered having made that change and unwound the issue.  If it had been the last thing I did for a long time on that page, I may never have made the connection.