开发者

Is there any reason to put code in the code behind rather than in the aspx file?

Any time I need to write code I almost always put it in the code behind file. For some reason this has always seemed like the "right" way to write code.

Based on most of the examples I see online, a lot of other people choose to write code this way too, but it seems like putting your code in the aspx file allows you to do all the same stuff as well as offering a few advantages.

Top of the list being:

  • Easier to make changes since no recompile.
  • You have access to all code if you can't find the project in source control (this recently happened to me).

Is there any benefit to having code in a code behind file? Is the开发者_如何学运维re any disadvantage to putting code in your aspx file?


Separating the code from the markup results in much cleaner structure, especially for non-trivial pages.
This is one of the major benefits to ASP.Net over ASP Classic.


Easier to make changes since no recompile.

Depends in what context, you can upload normal ASPX + ASPX.CS to the server and it will compile it for you on IIS.

I use this for my personal web site, I hate having to 'publish' the web site and then upload the files, sometimes on the server I need to make quick edits in Notepad, hence this approach is perfect.

I personally like the setup of keeping them separate.

PS. I use 'Web Sites' not 'Web Projects' which I believe has to be compiled before uploading to the server.


Maintainability. It allows the HTML part to be edited independently of the code, by and large.


When you have a very small project it is just a bad practice. They just tell you it's wrong, but it feels perfectly right.

You will hit the wall when you start working on big projects and especially when you will need to maintain them.

When your project gets big -the GUI part(the form) becomes smaller: one component here, this color there... not too complicated.

On the other hand, the logic part becomes a real pain... if I use a function in 20 pages will I change each replication of the function in each form? If they are almost identical, yet a bit different... how will I implement the solution?

OOP offers many design patterns so that your code will be as clean and efficient as possible... but you have to work with objects rather than a form.

A form is an object, yet it doesn't meet the OOP design patterns rules. And those rules are there to make your life easier.

I once had to maintain a huge system... and every change required changing many forms and testing many forms. After rebuilding the system it became a SMALL system and easy to maintain. It is easy to just build a form from scratch ignoring the entire system... but it's really hard to maintain this form.

Never think of the year you build the project, think of the 20 years you are going to maintain it.

Good luck Asaf


Stumbled upon this link now searching for the real advantages of putting code in cb files, and to confirm SLaks' answer, see this link for those who still wondered. Really thought processing and speed could've been an advantage as well, but alas...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜