Recently I have been working on a web project in VS2005.
All has been going well, several class files, several pages, and a few web controls all make for a project of fun.
I came back to work on the project, and I happily added a few more web controls, compiled the site etc and got some work done.
Now here's where things began to go wrong, after some lunch I reopened my project, but this time I opened the projects solution properly. Did some work, then went to build the solution and..... It broke. :( Now a project not compiling occurs frequently, ie rubbish code, but this time it started winging on how all my controls in the custom web controls that I built before lunch were now out of scope.
Frustrating. I started poking around and tried to find the issue, and I couldn't find anything. It was at this point that I looked at my .ascx file and not the .ascx.cs
With ASP.Net you can have code seperation. Content in one file, .ascx if a control and logic in another.ascx.cs. This is called code behind, and in the content .ascx file you normally have a reference that links the logic file, .ascx.cs to the content control.
This is where my issue was. ASP.Net 1.1 used the syntax CodeBehind, and the code in the file was classed as a seperate class. .Net 2.0 however introduced partial classes and as a result Microsoft added the CodeFile syntax. This syntax CodeFile doesn't include the full code-behind class declaration, instead this is generated on page compilation. The page compilation step automatically generates members for each control and these are then fused with the CodeFile's partial class.
My issue was that my file was using the CodeBehind syntax. Although this is still supported in .Net 2.0, during the page compilation step of the build the generation of the members partial class is missed out. .Net 2 assumes that the codebehind file is a full class, not a partial class.
Now this is ok, as long as your code behind file contains a full class and not a partial class .Net 2 will compile the code using the old method. But somehow, and I'm still not sure how my Content file, had changed to using CodeBehind instead of CodeFile. Initially I missed this as CodeBehind isn't necessarily wrong, in the correct context.
Changing codebehind back to codefile corrected the issue and the partial classes were generated and fused properly.
After searching the net I also found that this can occur after upgrading a vs 2003 .net 1.1 project to vs 2005 .net 2.0 Web Application project and seems to cause a few people headaches.
So there you have it, if you find your code won't compile suddenly or after converting a 1.1 project to a 2.0 Web Application project, with your controls being out of scope, check whether you're using codebehind with partial classes and if so change to CodeFile.