Scott on Writing

Musings on technical writing...

To Code-Behind or Not to Code-Behind: That is the Question!

The ASP.NET Dev Center on MSDN has started a Community Inbox where you can send in your questions to be answered by community leaders, such as Steve Smith, Andrew Duthie, Dino Esposito, and others.  The most recently discussed question was: Should I use Code-Behind or Code-Inside?  Code-Behind is the technique VS.NET forces you into when creating an ASP.NET Web application, whereas Code-Inside is the technique of using a server-side script block (<script runat=”server”>...</script>) in the HTML portion of the ASP.NET Web page.

Steve and Andrew chime in saying that Code-Behind is the way to go if you are using VS.NET, as it's the model the tool supports.  Scott Hanselman essentially suggests to do what you want, as Visual Studio .NET 2005 will offer native support for either model.  Dino has a dissenting opinion, stating that he's against Code-Behind.  His main complaint is as follows:

What I dislike of VS.NET code-behind is that is requires an extra, mandatory compilation step and that forces you to deploy an assembly. I agree that this may be a negligible issue in the context of an enterprise project, however. All in all, I feel bad when I see that a keyword found in each @Page directive of a page created with VS.NET means little to the runtime expected to process that page. The keyword in question is CodeBehind. It's there only for the VS.NET editing purposes and ASP.NET gently skips over it. Try removing it and you'll see that the page is correctly processed but you'll have a hard time trying to re-edit it through VS.NET. The problem with code-behind is not with the technique itself--which is great and from a certain perspective even due--the point is the VS.NET implementation of it.

I think Dino is scratching at the surface of why Code-Behind in non-ideal, but he's a bit off the mark.  It's not that Code-Behind is a good model, and there is just not sufficient tool support; rather, Code-Behind is a poor model chosen specifically because of the limitations of the tool (VS.NET 2002/2003).

I used to think Code-Behind was a decent programming paradigm.  Sure, it had some weaknesses, but it was the required approach for building Web applications in VS.NET.  It allowed for IntelliSense, debugging, etc.  These views were shaken up a bit after talking to Andy Smith at the MVP Summit last week.  Andy has just added a good entry on his blog titled Spaghetti, CodeInPage, CodeBehind, and CodeBeside, which articulates the points he made to me in Redmond.  A definite must read.  In addition to these shortcomings of Code-Behind, Andy also informed me that you could debug an ASP.NET Web page using the Code-Inside technique with VS.NET 2002/2003... granted, there's no IntelliSense, but debugging is possible.

Fortunately, this whole debate - and all associated confusion - over Code-Behind technique, is moot, as Visual Studio .NET 2005 will remove the need for Code-Behind as the editor will have IntelliSense in the HTML designer.  Also, Code-Beside offers a panacea for the ills introduced by Code-Behind.  As Andy points out in his blog entry, Code-Behind was a hack introduced to allow for ASP.NET Web page development in VS.NET 2002/2003.  Thanks to needed upgrades in VS.NET 2005, the need for Code-Behind should vanish.

posted on Wednesday, April 14, 2004 12:23 PM

Feedback

# re: To Code-Behind or Not to Code-Behind: That is the Question! 4/14/2004 12:40 PM Brian R. James

Huh? I thought the whole point of code-behind was to segregate the user-interface from the logic. Why would be want to go back to the old ASP days of in-page sphaghetti code?

# re: To Code-Behind or Not to Code-Behind: That is the Question! 4/14/2004 12:47 PM Scott Mitchell

Brian, when talking with Andy, that was my first comment. He mentioned a couple of things though:

(1) Even with code-behind you don't have true separation. Ex: use a TemplateColumn in a DataGrid - you have code, <%# ... %>, in the TemplateColumn's databinding syntax.

(2) WinForms don't separate code from content. In a WinForms class you have tool-generated sections that programmatically create the controls on the page and set their initial properties.

(3) The server-side script block does separate code from content. You have the code in one place - the server-side script block - and the content in another - after the script block (or before it, perhaps). True, they are not separated into two separate files, but code-behind is not instrinsically needed for this (just needed due to VS.NET 2002/2003). For proof, see the code-beside technique in VS.NET 2005.

# re: To Code-Behind or Not to Code-Behind: That is the Question! 4/14/2004 12:58 PM Brian R. James

You're right on with the WinForms example, but that is a problem that will hopefully be corrected with XAML. IMHO, the structure/visual layout of a page should be separate from the logic that controls the page. WinForms fails miserably in this area.

While, it's true that the server-side script block serves a similar function as a code-behind, I would argue that a separate file is still the preferred method. For the same reasons that one would want to move css into an external stylesheet and javascript into an external script file. A designer should only have to look at HTML and CSS. A scripter should only have to see the javascript, etc...

# re: To Code-Behind or Not to Code-Behind: That is the Question! 4/14/2004 1:08 PM Michael Teper

I agree with everything you and Andy say about the shortcomings of code-behind, but to me the benefit of separating code and UI is worth it. Particularly, in the ability it gives me to compile code before putting it out to the world (I haven't checked the CTP bits yet, but this was missing from the code-beside model in the PDC build).

I don't consider the databinding productivity hack a valid counter-argument: its a horrible syntax with zero compile-time validation and a convoluted model. It makes certain simple things easy but totally breaks down beyond that. There days, I avoid it as much as possible.

I agree with Brian that the fact that WinForms doesn't do a better job of separating UI from code is just a problem with WinForms and it does look like XAML is going to address that.

# re: To Code-Behind or Not to Code-Behind: That is the Question! 4/14/2004 1:25 PM Scott Mitchell

Michael, separating code and content into separate files isn't a prerequisite for all - just for those who have different designers/coders. And VS.NET 2005 allows for this.

My main point (and Andy's, I believe, although I don't want to put words in his mouth), is that Code-Behinds exist as they do because of the weakness of VS.NET 2002/2003. Essentially, they are a hack to allow for the "benefits" of Code-Behind in VS.NET 2002/2003. These same benefits can be realized in a much nicer manner with an adequate tool. To put it another way, no one would use Code-Behind if VS.NET didn't force them to. Ergo, Code-Behind is far from ideal. Thankfully, these ills are being addressed with Whidbey.

Furthermore, Code-Behind has several disadvantages. All code-behind classes are compiled into one assembly, meaning ALL pages and ALL User Controls in the project must be of the same language. There is only a very weak connection between the Web controls in the ASPX page and the associated class instances in the Code-Behind class (namely, the implicit mapping of ID values).

Finally, Michael, my point about databinding is that it is proof that you can't really have a truly separated code from content. I don't know if we're talking past each other here, or if I'm not understanding your point exactly.

# re: To Code-Behind or Not to Code-Behind: That is the Question! 4/14/2004 3:50 PM Mike Singer

This is VS.NET 1600 (codenamed
"Shakespeare") auto-generated report:

-- syntax error:

-- "To Code-Behind or Not to Code-Behind: That is the Question!"

-- commas expected

-- example: To be, or not to be, that is the question,

-- source: Longman, 1982

# re: To Code-Behind or Not to Code-Behind: That is the Question! 4/14/2004 5:56 PM Scott Mitchell

Mike, in the future if you want to alert me to grammatical errors, could you please do so via the Contact page? [http://scottonwriting.net/sowblog/contact.aspx]

I'd like to keep the comments for a blog entry to those that are focused on the discussion.

Thanks.

# re: To Code-Behind or Not to Code-Behind: That is the Question! 4/15/2004 10:31 AM Scott

Scott (is there an echo in here?),
How does Whidbey address the problem of embedding data binding syntax in the aspx files?

# re: To Code-Behind or Not to Code-Behind: That is the Question! 4/19/2004 10:43 AM Greg Pyatt

I don't doubt for a moment that your assertions about code-behind's failings are entirely accurate. However, speaking as someone whose done ASP.NET projects and subsequently done ASP classic, I can tell you that you really, really, *really* don't want to go back to the bad old days of spagetti-code. Sure, code-behind has its problems and the CODEBEHIND attribute is definitely lame, but it's a good move in the right direction. I suppose MS should feel a bit ashamed for hoisting upon its developers the code-behind for ASP.NET, but I think in many ways it helped VBScripters migrate to a cleaner, more OOP-ish style of coding in VB.NET.

# re: To Code-Behind or Not to Code-Behind: That is the Question! 4/19/2004 10:48 AM Scott Mitchell

Greg, I'm not taking MS to task for code-behind - it was the best they could do with the tool (VS.NET) provided. With VS.NET 2005, though, the problem is solved, which is nice.

If you haven't already, check out this article in the upcoming MSDN Magazine:

Create Elegant Code with Anonymous Methods, Iterators, and Partial Classes
http://msdn.microsoft.com/msdnmag/issues/04/05/C20/default.aspx

Pay particular attention to the bit of partial classes, which is a new feature in Whidbey that allows you to define bits and pieces of a single class in different files. With partial classes, you can have what is called "code-beside" in ASP.NET 2.0, which is a much cleaner model than code-behind, which uses an auto-generated class that is *derived from* the code-behind class.

# re: To Code-Behind or Not to Code-Behind: That is the Question! 4/21/2004 9:05 AM Kent Watson

Scott, If you are using <%# ... %>, in your .TemplateColumns, then you really don’t know how to properly use code-behind. You can easily manipulate any object by setting up the OnItemDataBound event handler. So you really can get UI and code separation if you structure your code well.

Inline code is great for programmers that do not know what they are doing, or for programmers that are still in the ASP mindset. If you are able to understand the greatness that Microsoft has provided in ASP .NET, you will come to love code-behind. It gets even better when you create Assemblies that contain common functions and Custom Controls (although I wish there was a better way to re-GAC Assemblies on multiple servers).

Code-behind enables Object Oriented programming in a beautiful way. Hopefully the inline spaghetti programmers will go to some training classes, and find out how take advantage of .NET.

However, I will counter that if you writing say an intranet website instead of a self contained web application, you may not want to create separate projects and applications for each and every department and sub department if you are just adding a little code here and there… inline code has an advantage there, of course, your really don’t need VS .NET 2002/2003 to do that… notepad or FrontPage will suffice. For those of us creating real web applications, there is not a better tool or programming model out there than using VS .NET, C#, Assemblies, and code-behind to create managed code.

# re: To Code-Behind or Not to Code-Behind: That is the Question! 8/13/2004 12:41 AM Anand Bisht

Hi All,

I just went through the debate/discussion over the code-behind or not to be code behind. The direction which Microsoft took is in right direction and I hope near future or VS2005 will solve some of these issues but again they are introducing many more like class diagram etc under one roof. But lets see how far its work. -:)

Candidly speaking VS.NET is great tool ever I have seen I hope no one will disappoint me in this regards. Finally lets work together and come up with final conclusion of the new era.

If somebody give me to design the IDE for the new era I would definitely think beyond the IDE. IDE which would design the architecture rather then just editing and compiling. IDE which would support designing the ERP/CRM solution.

And I hope those days are not far, because all you guys are here to do that.

thanks and regards
Bisht

# re: To Code-Behind or Not to Code-Behind: That is the Question! 9/10/2004 2:53 AM Pesch

Hi, My main concern with the code-beside idea and the new compilation model i asp.net 2.0 is the fact that all or nothing is compiled.
i´d prefer the way it is in asp.net 1.x where the code-behind becomes a .dll in the /bin-directory and the html/UI stays the way it is. since the "view source" feature is availble in the browsers the argument that the html has to be protected as it´s a part of your intellectual property doesn´t really apply.

i´d like the option to compile all code (like the new compilation model/precompilation) or just the code-behind/code-beside code. when compiling it would also be great if you could build one or many assemlies.
i´d also like if, in VS, i could choose the code-behind or the code-beside, as both clearly has advantages.

am i on the right track or just confusing?

# re: To Code-Behind or Not to Code-Behind: That is the Question! 9/17/2004 3:38 PM Rajeev Gopal

http://rajeevgopal.blogspot.com/

# re: To Code-Behind or Not to Code-Behind: That is the Question! 1/27/2005 2:25 PM Anatoly Lubarsky

Bullshit

May be you should try good old asp ?

One point though: "Code Beside" is the 2005 version of "Code Behind". Inline shit remains inline shit. The reason for the model is partial classes concept and the fact that both classes are compiled in design time. Because of that the dynamic intellisense is working and not because of the "revising the concept".

# re: To Code-Behind or Not to Code-Behind: That is the Question! 7/26/2005 1:30 PM Mrs. Nussbaum

In Classic ASP multiple programmers could code the same site at the same time. How do you do this in ASP.Net when the entire site has to be compiled to one file? Well you could use codeInPage and have it compiled on the fly. Change the one file you're working on and that's it. Nice feature when changing "teh" to "the."

Visual Studio might as well be notepad when you use codeInPage. To make things extra fun IIS will restart the entire site if you update codeInPage files past the default 15 times. And as a bonus you can't press "play" in Visual Studio to get that cool debuger.

# re: To Code-Behind or Not to Code-Behind: That is the Question! 8/31/2005 7:05 AM Mark Kamoski

I cannot believe it, Scott. From you? I simply love the 4Guys site. I never new it was you. Now, sadly, my opinion has dropped. I simply cannot believe that you prefer inline-code to code-behind. It is utterly baffling, despite the specious arguments that everyone has mentioned.

I cannot imagine what a non-trivial site, (one with a complete object hierarchy mapped to well-planned domain model), would look like if it were all inline-code. I shudder at the thought.

BTW, if you want to see an example of GREAT separation of presentation-logic from processing-logic, look no further than the UI Application Block from Microsoft. It has a darn good implementation of MVC.

...anyway...

Oh well, I am not going to convince you and I do not even want to try.

However, I do want to respond to something you mentioned. You stated the following...

"Dino has a dissenting opinion, stating that he's against Code-Behind. His main complaint is as follows: What I dislike of VS.NET code-behind is that is requires an extra, mandatory compilation step and that forces you to deploy an assembly"

...and I want to say that this is not entirely correct. That is, there are at least 2 ways to implement the code-behind technique in ASP.NET as of now. One way is to use the "Codebehind" attribute of the @Page directive, as VS.NET 2003 does. Another way is to use the "Src" attribute of the @Page directive, which unfortunately VS.NET 2003 complains about but it can be done. This has other implications, such as the fact that the source code needs to be on the server-- but, if one is using in-line code, that is also the case. Personally, I do not see any issue with putting the code on the server. One can use a simple FTP program to run XCopy deployment.

To see the 2 ways of implementing code-behind, there is a sample at http://www.WebLogicArts.com/S07/DemoList.aspx">http://www.WebLogicArts.com/S07/DemoList.aspx for those who don't know this syntax.

Well, that's it. I just wanted to state my incredulity and also make that remark about the 2 ways to do code-behind.

(Scott, just remember, it is never too late to turn back. Code-behind loves you-- more than you know.)

:-)

Thank you.

--Mark Kamoski

http://www.WebLogicArts.com

# re: To Code-Behind or Not to Code-Behind: That is the Question! 8/31/2005 7:36 AM Mark Kamoski


I have crafted another reply at the ASP.NET forums, at the following link...

http://forums.asp.net/ShowPost.aspx?PageIndex=2&PostID=1038002#1038002

...for any who are interested.

# [PLEFDS] Inline, beside, behind, Web Site Project, Web Application Project, y bla bla bla... 2/21/2008 10:30 AM SergioTarrillo's RichWeblog

De ahora en adelante [PLEFDS = para leer el fin de semana]. Digamos que hay dos maneras de tener el c&#243;digo

# Inline, beside, behind, Web Site Project, Web Application Project, y bla bla bla... 3/23/2008 1:01 PM .net y algo mas

Todo acerca de Inline, beside, behind, Web Site Project y Web Application Project. De ahora en adelante

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

Add To Your Reader

My Links

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<May 2008>
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Comment Stats

DayTotal% of Total
Sunday 1866.8%
Monday 37913.9%
Tuesday 45316.7%
Wednesday 50418.5%
Thursday 53519.7%
Friday 49418.2%
Saturday 1666.1%
Total 2717100.0%

Hour1Total% of Total
12:00 AM 652.4%
1:00 AM 682.5%
2:00 AM 622.3%
3:00 AM 742.7%
4:00 AM 572.1%
5:00 AM 1033.8%
6:00 AM 1084.0%
7:00 AM 1585.8%
8:00 AM 1716.3%
9:00 AM 1475.4%
10:00 AM 1716.3%
11:00 AM 1816.7%
12:00 PM 1886.9%
1:00 PM 1696.2%
2:00 PM 1605.9%
3:00 PM 1324.9%
4:00 PM 1073.9%
5:00 PM 923.4%
6:00 PM 913.3%
7:00 PM 963.5%
8:00 PM 833.1%
9:00 PM 782.9%
10:00 PM 792.9%
11:00 PM 772.8%
Total 2717100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.54144
Monday 5.22339
Tuesday 4.28419
Wednesday 7.67637
Thursday 6.90607
Friday 5.48411
Saturday 5.33160
Total 5.842717

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 7.0035
8:00 AM 5.35107
9:00 AM 6.32278
10:00 AM 6.47246
11:00 AM 4.41181
12:00 PM 6.88330
1:00 PM 3.00111
2:00 PM 5.41222
3:00 PM 8.64285
4:00 PM 4.0589
5:00 PM 5.92154
6:00 PM 4.52113
7:00 PM 9.67174
8:00 PM 9.80147
9:00 PM 5.05111
10:00 PM 5.4265
11:00 PM 4.5732
Total 5.842717

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles