Code-Behind or Single File?

The latest OdeToCode article: The Code Models Of ASP.NET 2.0.

The obvious question is which model should you be using for your ASP.NET projects? The answer will largely depend on the type of person you are. Working from a single file containing both the code and ASPX markup will appeal to many people, while others will insist on a strict separation and favor the code-behind model. The single-file model has an advantage in configuration management and deployment, since there is only a single file to version and deploy. Intellisense and refactoring tools appear to work equally well with both models, so there will be no clear winner in the productivity category. One additional factor in deciding on the model to use is your pre-compilation strategy, which will be the topic of our next article...

I have some more thoughts on the subject to blog later this week, as there are some subtle but suprising differences, particularly in VB.NET versus C# land....

Teaser, teaser, teaser. It's all about the teasers.

posted on Thursday, June 02, 2005 12:33 AM by scott

Comments

Thursday, June 02, 2005 6:45 PM by

# Code-behind for Me

Tuesday, August 09, 2005 9:12 AM by Kilambi

# re: Code-Behind or Single File?

Hello,

When I converted ASP to ASP.NET using the Migration Tool(Microsoft) I get only a single file. Here in the file I need to call a WebService and use the dataset to populate the fields in the page. Do you have any solutions for this. I need all the code behind in a single aspx file

thanks
Tuesday, August 09, 2005 10:42 AM by scott

# re: Code-Behind or Single File?

Hi Kilambi:

What is the error or problem you are seeing. You say there is only a single file, but I don't see in the description of the problem what you are missing, exactly.

Have you tried the latest migration tool?

http://www.virtualgoldfarb.com/samples/NewMigration2.zip
Tuesday, November 08, 2005 8:13 AM by RM

# re: Code-Behind or Single File?

Hi, great article.. just wondering how you best approach c# event handlers with the new partial classes.

Is there an easy way to add events to controls in the aspx page? e.g. for a repeater databound event do you need to just have to manually call a method Repeater1_DataBound(..) In VS2003 i would typically go into the auto-generated code section and use the intellisense to add the new method for me using the tab key. This functionality is still there - but obviously there is no longer any auto-generated section.

Thanks.
Friday, November 11, 2005 6:24 AM by scott

# re: Code-Behind or Single File?

I wouldn't manually call the method.

If you are using a single file (no CodeFile or code-behind), the easiest method is probably to use the drop down lists above the editing window to add event handlers.

If you are in a CodeFile, you can always override FrameworkInitialize just like in 1.1 and add the event handlers with += and TAB completion. This approach works with or without CodeFile.

Then of course you can always double click on an event in the Properties window for a control. That will wire things up and add a declaration for you.
Tuesday, November 22, 2005 11:57 AM by mj

# re: Code-Behind or Single File?

Nice artile. Thanks!

I think one of the things to think through is how you are going to deploy your application and versioning (upgrading). I'm not sure that I like the idea of having the soure code deployed with the application so that the "runtime" can compile it.

I'm wondering if this means that you could modify the source and copy the source file to the web application directory and the compiler will "recompile" that page and also the app .dll? -- kind of like old .asp pages :)

I'm sure there are some other considerations for using partial classes...I'll need to dig a little deeper.

Wednesday, November 23, 2005 6:20 AM by scott

# re: Code-Behind or Single File?

mj: Yes. You can put everything on the server and ASP.NET will recompile it all.
Wednesday, January 25, 2006 10:55 AM by Peter B

# re: Code-Behind or Single File?

Hi,

I want to add my own namespace instead of letting .NET put my page under the ASP namespace.

Is there someway to do this? I haven't been able to find a property, etc, to override the default namespace assignment. For the record, I think this is kinda nutty.

Is there any documentation you can refer me to to help me understand why MS chose to implement Web pages, and to a lesser extent custom classes with no default support for namespaces, and the rationale behind the default class naming?

This is the complete opposite of winforms, and it will more than likely cause some problems with code reuse, with respect to BO's, from one technology to the other.

Thanks,
Peter
Thursday, February 16, 2006 2:45 AM by Gurvinder

# Wireup

Can u please explain with correct example for autowireup directive in true or false case.

Thanks
Gurvinder
Thursday, June 15, 2006 6:20 PM by Ryan

# re: Code-Behind or Single File?

I have an application that uses the single page model, but I cant get the intellisense to work. Is there a setting in vs 2005 that makes all pages single model? Also, can I double click a for object and it create the event handler automatically in the single page model? Thanks for any help in advance!

Great article by the way!

Ryan
Thursday, June 22, 2006 8:22 AM by jingyoo

# Thank for your posting

I have read your article through asp.net site. I appreciate it, and can i have a question?
You show me an excerpt of the file generated by parsing the ASPX, but i can't find it.
How can i view this code?

Thanks in advance,
Jingyoo
Thursday, June 22, 2006 8:27 AM by Jingyoo han

# re: Code-Behind or Single File?

Please tell me how to view the source code in Temporary asp.net files directory.

Thanks in advance,

Jingyoo.
Thursday, June 22, 2006 1:14 PM by scott

# re: Code-Behind or Single File?

Jingyoo: Set debug="true" in web.config and you'll find the files in the temporary asp.net files directory.