CodeFile and CodeBehind in .NET: What the heck is the difference?


If you've enjoyed reading this post then please subscribe to my Full Text RSS Feed.

The Single-File Page and Code-Behind Page Models

The Single-File Page Model Example

NewPage.aspx declaration

<%@ Page Language=”C#” %>

The Code-Behind Page Model Example

NewPage.aspx declaration (.NET Framework 3.5)

<%@ Page Language=”C#” CodeFile=”SamplePage.aspx.cs” Inherits=”SamplePage” AutoEventWireup=”true” %>

NewPage.aspx declaration (.NET Framework 1.1)

<%@ Page Language="C#" CodeBehind="SamplePage.aspx.cs" Inherits="SamplePage" AutoEventWireup="true" %>

Difference between CodeFile and CodeBehind

ASP.Net 1.1 used CodeBehind, and the code in the file was classed as a separate class. However in .Net 2.0, Microsoft introduced partial classes and added CodeFile. 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.

Have a read about “So What is it Code Behind or Code File??” blog article for more information:

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

My experience using CodeBehind and CodeFile in .NET Framework 3.5

When I upgraded my ASP.NET web application from .NET 1.1 to .NET 2.0 and then to .NET 3.5 using Visual Studio, the upgrade retained
Codebehind in all your ASP.NET pages. After the upgrade, I added a new button control to the page, I suddenly noticed that my ASP.NET
page was behaving like the “Single-File Page Model”. It’s when I replaced CodeBehind with Codefile, that my ASP.NET page recognised
that I am asking it to use the Code-Behind page model and not the Single-file page model.

Anyone has other experiences to share about CodeFile and CodeBehind?

Sphere: Related Content

SPONSORED LINK: Would you like to be a sponsor? To find out more, get in touch with us.

Related Posts

About the Author

a tech junkie and a software developer. a apple fan and an avid photographer. a frequent traveller and loves art and graphic novels. My Google+