ASP.NET CodeFile, CodeBehind and Inherits

孤街浪徒 提交于 2021-01-27 05:04:29

问题


My confusion is not new here or arround the web, yet, i have some questions for which i did not find answers anywhere:

The first question is:

Why is Inherits necessary on CodeFile and not on CodeBehind?

I read: http://msdn.microsoft.com/en-us/library/vstudio/ms178138(v=vs.100).aspx and some more pages, and i understand that CodeFile is for source code and for compilation on the fly while the other is for an assembly. This raised me another question:

Why do everyone say that CodeBehind must be an assembly if i find File.aspx.cs everywhere including in a test project i have and it works like a charm? Is this a compiled assembly?

But, as of the first question, and based on the url i supplied, i might understand, why the Inherits is necessary. I assume the compiler must know what is the name of the partial class to compile later. Makes sense. Yet, shouldn't it be necessary on CodeBehind as well? If we are going to merge the partial classes, i suppose i should give the name of the one i want to merge. I even tried adding another partial class to the .cs file and it compiled/ran well.

Am I missing something here?

I also read that CodeBehind is not used anymore and it's CodeFile, the new one.

Any .NET guru to help me?

Thank you all in advance.

UPDATED:

I tried to implement events using no Inherits on CodeBehind. It didn't work. This makes more sense. But, it doesn't complain on compilation, while CodeFile does. Is there any reason for this?


回答1:


Inherits property carry the class name which written inside the code behind file. you can have multiple classes in same code behind file and inherit in two different .aspx file




回答2:


CodeBehind file provides the code behind for the aspx page. When you add a new "ASP.NET webpage with CodeBehind", let's call it NewPage.aspx, 2 files are also created, called NewPage.aspx.cs and NewPage.aspx.designer.cs. NewPage.aspx.cs is your CodeBehind file. As Vijay stated, since a (C#) file can have multiple classes (which is different from Java, where a class file can only contain 1 class, and the class name have to be matching the file name), Inherits=[namespace].[class] indicates which class you want to actually use for the web page. The CodeBehind attribute is for Web application projects. The CodeFile attribute, on the other hand, is for Web site projects.



来源:https://stackoverflow.com/questions/21840504/asp-net-codefile-codebehind-and-inherits

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!