Soldarnal
Soldarnal

Reputation: 7798

CodeFile vs CodeBehind

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?

Upvotes: 149

Views: 105889

Answers (3)

Shafqat Ahmed
Shafqat Ahmed

Reputation: 2072

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.

Upvotes: 176

DavidHyogo
DavidHyogo

Reputation: 2896

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.

Upvotes: 6

Ranjeet Prasad
Ranjeet Prasad

Reputation:

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

Upvotes: 5

Related Questions