Real Difference Between CodeFile vs CodeBehind

I’ve recently got the task to convert the existing VS 2003 application written in VB.NET to VS 2005. After performing some evaluations, I decided to do it manually for each page, because, in my opinion, since the target application is also using the same language, i.e. VB.NET, the effort involved could’ve be minimal.

So, I grab one sample page, the usual default.aspx page to get a feel of what amount of efforts to be taken in the conversion process. Because I want to make this page the same configuration as my existing VS 2005, I first make some modifications to the header of default.aspx page and change the class declaration to partial class declaration.

After performing some modifications, I am firing the page and got this error :

So, the error message is “Could not load type _Default”.

But, I’ve checked and rechecked the declaration of class name between the code page, that is located in separated default.aspx.vb, the name is exactly the same. So I think there is some other issue involved here.

The next thing I’ve done is to create the new form using VS 2005 editor, called the testing.aspx, and runs it without any problem.

After creating the backup of existing default.aspx, and then replaced it with the generated code from testing.aspx, of course the error is gone.

So what kind of statement that causes the default.aspx failed ?

By performing the successive elimination of the statements, I eventually arrive at just one line of code in the .aspx file :

The failed page :

The success page :

At a glance, these two lines looks just the same, but paying closer attention will reveal that the failed page use the “CodeBehind” keyword, as for the success one used the “CodeFile” keyword.

So, how about if I changed the partial class statement :

Partial Class _Default
Inherits System.Web.UI.Page
End Class

Into :

Public Class _Default
Inherits System.Web.UI.Page
End Class

Inside the default.aspx.vb. Does the CodeBehind keyword works in VS 2005 ?

Nope, it doesn’t. The error is the same. So, I conjectured, that the CodeBehind statement requires compiled application’s DLL in order to be activated successfully, regardless of the class declaration statement inside its *.vb source code file, i.e. it doesn’t care whether you declared it as partial or public. Anyone wants to prove my conjecture ? 🙂

So the solution in this context, when you’re not using the DLL in your application in the conversion process, just stick to change the “CodeBehind” to “CodeFile” and off we go 🙂

Advertisements

One Response to “Real Difference Between CodeFile vs CodeBehind”

  1. amit kumar sinha Says:

    Nice post

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: