简体   繁体   English

代码旁边和代码背后的区别

[英]Difference between code beside and code behind

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

CodeInPage : which means putting our code into our page. CodeInPage :这意味着将我们的代码放入我们的页面。

CodeBehind is a separate file for the code. CodeBehind是代码的单独文件。 This file derives from Page, contains declarations for the server controls, and contains all the event handlers and such. 此文件派生自Page,包含服务器控件的声明,并包含所有事件处理程序等。 The aspx file then derives from this class for the final page. 然后,aspx文件从最后一页的此类派生。
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. CodeBehind解决的两个问题是intellisense每个文件需要1种语言才能工作,因此服务器端代码放在一个文件中,我们很高兴。 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. 它还减轻了编译器检测服务器端代码中的错误的痛苦,因为它只需要自己处理代码文件,而不是混合的ui声明。

Code-Beside allows one class to be defined in multiple source files. Code-Beside允许在多个源文件中定义一个类。
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 ASP.NET v2.0:Code-Beside取代了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. 本文解释了在ASP.Net 2.0中引入了使用部分类模式的代码,以替换用于.Net 1.0的模型背后的代码 - 但是因为每个人仍然调用分离的代码 - .aspx.cs或.aspx.vb - 代码背后现在没有实际的区别。

In asp.net 1.0 development you had to declare every control used in the aspx page in the code behind; 在asp.net 1.0开发中,你必须在后面的代码中声明aspx页面中使用的每个控件; 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. 所以也许最初的重命名是因为模型背后的代码在.net 1.0中不稳定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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