tags:

views:

5039

answers:

3

What is the difference between CodeFile="file.ascx.cs" and CodeBehind="file.ascx.cs" in the declaration of a ASP.NET user control?

Is one newer or recommended? Or do they have specific usage?

+7  A: 

CodeFile is newer, it's introduced with ASP.NET 2.0, while CodeBehind is there from the start.

A longer explanation on what each of the names does can be found at this good blog post:

http://blog.mjjames.co.uk/2008/02/so-what-is-it-code-behind-or-code-file.html

It all comes down on what gets compiled, when and where.

More resources at: http://www.pluralsight.com/community/blogs/fritz/archive/2005/01/18/5111.aspx

David Cumps
A: 

Codebehind file need to compile before run but in src we dont need to compile and then run.. just save the file.

+7  A: 

CodeBehind = Needs to be compiled ( asp.net 1.1 model) and compiled binary is placed in the bin folder of the website. You need to do a compile in visual studio before you deploy. Good model when you do not want the source code to be viewable as plain text ... for example when delivering to a customer who you not have obligation to provide code.

CodeFile = You provide the source file with the solution for deployment. ASP.NET 2.0 runtime compiles the code when needed. The compiled files are at Microsfot.NET[.NET version]\Temporary ASP.NET Files.

Shafqat Ahmed
So, does this mean that the developer must manually change the attribute of the Page directive or is there a way to create a web project which selects codebehind or codefile, by default, each time a new web page is added?
Matt W
The way i see it is CodeFile is meant to be used for a "WebSite" and "Codebehind" is meant for a "Web Application Project" as it needs to be compiled. So while converting a "Website" to a "Web Application Project" we may need to manually change all CodeFile occurences to Codebehind!
renegadeMind