ASP.NET 2.0: To use or not to use code behind?

  • Thread starter pedestrian via DotNetMonster.com
  • Start date
L

Laurent Bugnion

Hi,
I'm just beginning to learn ASP.NET 2.0. I wonder what's the reason for
using or not using the code behind file?

Thanks for replying...

Do you mean using code behind as opposed to writing the code inline in
the ASPX page?

Though this last possibility exists, it should not be used except in
very specific cases (of which I cannot even imagine one).

An ASPX without any code behind is also possible. For example, yu might
want to use an ASPX page to display HTML code and custom (or user)
controls only, in which case the ASPX page's code behind file may be
deleted.

In all other cases, it is recommended to use code-behind files to
implement your server-side logic, and not inline. There are many
advantages to that:

- Your CS code will be compiled in DLLs, instead of being placed in
clear text on the server. The DLLs may even be encrypted if your firm
wishes to protect the code.

- It allows a much better encapsulation of the code, and allows a better
OO design.

- Maintainability is better when the C# code, CSS code, JavaScript code
and ASPX code are in different files. It's easier to identify and modify
files this way, instead of having everything in the same file.

- I am sure there are more.

HTH,
Laurent
 
L

Laurent Bugnion

Hi,
Thanks for your reply, Laurent. I haven't told that I'm using VB2005 for
ASP.NET 2.0... Thanks.

You mean VB.NET, I suppose. Anyway, it doesn't matter, everything I said
applies to VB.NET as well as C#.

Greetings,
Laurent
 
S

Steven Nagy

(of which I cannot even imagine one).

Repeaters and ItemTemplates.

I personally would never rule out using inline code, along with any
other language feature. Your point about clear text code in your ASPX
is taken, but I can't say it should be a negative for just one side of
the argument. DLL's are decompilable, and if the bad people are
wandering around inside your server then inline code is not your
biggest concern. Lets assume a hacker compromises your server and has
access to your aspx files, as well as DLLs. They're most likely to copy
the entire site regardless and decompile your DLL. Inline code is no
more of a threat than code behind.

Maintainability is still highly relevant though...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,187
Latest member
RosaDemko

Latest Threads

Top