简体   繁体   中英

Difference between code beside and code behind

谁能告诉我Asp.NET中的代码和代码背后有什么区别?

CodeInPage : which means putting our code into our page.

CodeBehind is a separate file for the code. This file derives from Page, contains declarations for the server controls, and contains all the event handlers and such. The aspx file then derives from this class for the final page.
The two problems that CodeBehind solves is that intellisense needed 1 language per file to work, so the serverside code was put in one file and we are happy. It also eases the compiler pain of detecting bugs in serverside code, as it only needs to deal with the code files by themselves, not the ui declaration mixed in.

Code-Beside allows one class to be defined in multiple source files.
The main intended use for Partial Types is to allow code generators to create a class that can be extended in a separate file to not mess up any re-generation.


Refer to the following article:

ASP.NET v2.0: Code-Beside Replaces Code-Behind

This article explains that code beside using the partial class pattern was introduced in ASP.Net 2.0 to replace the code behind model used for .Net 1.0 - but as everyone still calls the separated code - .aspx.cs or .aspx.vb - code behind there is no practical difference now.

In asp.net 1.0 development you had to declare every control used in the aspx page in the code behind; and for some reason this was flaky and the event wiring kept being deleted after a solution was compiled. So perhaps the initial rename was because the code behind model was unstable in .net 1.0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM