CodeFile vs CodeBehind
Asked Answered
W

3

148

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?

Weinberg answered 16/9, 2008 at 14:37 Comment(0)
B
176

CodeBehind: Needs to be compiled (ASP.NET 1.1 model). The compiled binary is placed in the bin folder of the website. You need to do a compile in Visual Studio before you deploy. It's a good model when you don't want the source code to be viewable as plain text. For example when delivering to a customer to whom you don't have an 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 Microsoft.NET[.NET version]\Temporary ASP.NET Files.

Bratwurst answered 5/6, 2009 at 8:46 Comment(5)
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?Fortunna
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!Deel
CodeFiles are nice during development because you can test changes after simply saving and refreshing a page and without having to Build your project. You can still optionally "Publish" the .Net 2+ CodeFile compilation model files as DLLs if you don't like posting .cs and .vb files to a production server. When you Publish your project, you can optionally select "Precompile" options to turn either or both of your .aspx files and .cs/.vb files into DLLs, effectively like CodeBehind.Bryner
As renegadeMind pointed out (and from the upvotes it gets), I think the response should be edited to stress the fact that CodeFile is deeply linked to solution of type "website" whereas CodeBehind is linked to "web application project". Unfortunately my edit has been rejected.Dealate
the fact that codebehind is associated with asp 1.1 while codefile is linked to asp.net 2.0 could link to think codefile are "superior" to codebehind when they are not (and IMHO this is exactly the reverse : web applications are better than websites)Dealate
B
6

I'm working with an Application Project in Visual Studio Express 2012 For Web and using .NET 4.0. In the code behind files for my login and change password pages I found a situation where I needed both CodeBehind and CodeFile in the declaration.

If I don't add a code file reference like

CodeFile=login.aspx.cs

The web page doesn't parse and the browser displays a parser error. It doesn't matter whether I compile the project or not.

If I don't add a code behind reference like

CodeBehind=login.aspx.cs

References to Security classes like MembershipUser fail both at compile time and when attempting to use intellisense with an error like "The type or namespace MembershipUser cannot be found". I have added a reference to System.Web.ApplicationServices as required by the .Net 4.0 framework.

I should add that these troublesome files are running in an application within the website created using the IIS Application tool. When I open the website from Visual Studio I have no difficulty with parser errors or reference errors. This confusion only occurs when I open the application as a project in Visual Studio.

Buschi answered 9/6, 2013 at 3:29 Comment(1)
I know what you mean John. And maybe it deserves the downvote, but I'm really struggling with this problem at the moment. Maybe my difficulties don't come from this difference but from something else. However, although the accepted answer answers the question, it doesn't help with the practical problem I face. Maybe I should open a new question.Buschi
S
5

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

Sentimentality answered 23/5, 2009 at 11:7 Comment(1)
but in src - you mean but in the CodeFile?Hargett

© 2022 - 2024 — McMap. All rights reserved.