ASP.NET CodeFile,CodeBehind和继承 [英] ASP.NET CodeFile, CodeBehind and Inherits

查看:84
本文介绍了ASP.NET CodeFile,CodeBehind和继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的困惑在这里或在网络上并不陌生,但是,我有一些问题,在任何地方都找不到答案:

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

第一个问题是:

为什么在CodeFile而不是CodeBehind上需要继承?

Why is Inherits necessary on CodeFile and not on CodeBehind?

我阅读: http://msdn.microsoft.com/en-us/library/vstudio/ms178138(v=vs.100).aspx 和其他页面,我了解CodeFile是用于源代码和动态编译的,而另一个是用于程序集的.这给我提出了另一个问题:

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:

为什么每个人都说如果我发现CodeBehind必须是一个程序集到处都有File.aspx.cs,包括在我拥有的测试项目中奇迹般有效?这是经过编译的程序集吗?

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?

但是,从第一个问题开始,根据我提供的url,我可能会理解为什么需要Inherits.我假设编译器必须知道稍后要编译的部分类的名称.说得通.但是,在CodeBehind上是否也没有必要吗?如果我们要合并部分类,我想我应该给出我要合并的部分类的名称.我什至尝试将另一个分部类添加到.cs文件中,并且编译/运行良好.

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.

我在这里想念东西吗?

我还读到不再使用CodeBehind,它是新的CodeFile.

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

有任何.NET专家帮助我吗?

Any .NET guru to help me?

谢谢大家.

已更新:

我尝试在CodeBehind上不使用任何继承来实现事件.没用这更有意义.但是,它没有在编译时抱怨,而CodeFile却在抱怨.有什么理由吗?

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?

推荐答案

CodeBehind文件为aspx页面提供了后面的代码.当您添加新的带有CodeBehind的ASP.NET网页"时,我们将其称为NewPage.aspx,还将创建2个文件,分别称为NewPage.aspx.cs和NewPage.aspx.designer.cs.NewPage.aspx.cs是您的CodeBehind文件.正如Vijay所述,由于(C#)文件可以具有多个类(与Java不同,Java的类文件只能包含1个类,并且类名必须与文件名匹配),因此 Inherits = [名称空间].[class] 表示要实际用于网页的类. CodeBehind 属性用于Web应用程序项目.另一方面, CodeFile 属性用于网站项目.

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.

这篇关于ASP.NET CodeFile,CodeBehind和继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆