Tuesday, 19 February 2008

So What is it Code Behind or Code File??

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.

7 comments:

ganesh said...

Hi, Is there a way to make VS 2008 use the CodeFile by default, instead of codeBehind (when a new project is created)?

Thanks

Lucy said...

Do you need to recompile/build the application after changed from codebehind to codefile.

Michael James said...

Hi Lucy,

Yes you will need to recompile, so that the partial classes are created.

I also noticed since changing my theme this post has lost all its formatting so will re add it

Greg said...

hey thanks for this - definitely helped clarify the difference to me :)

Stuffinator said...

this solves a lot of problems I ran into with asp.net in the past and it will definitely help me in the future.

big thanks and keep up the good work. :)

greetings from germany

Anonymous said...

Thank you Michael,

You are a saviour.

Best,
Saeb

Dewang said...

thank you! very much for explanation. it helped!!!!!