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