首页 新闻 搜索 专区 学院

ASP.NET 2.0里aspx和cs两个文件之间的关系?

0
悬赏园豆:10 [已解决问题] 解决于 2008-09-27 18:15

ASP.NET 2.0中的partial,实际上是把存储于不同文件中的HTML和code behind编译成一个单独的Web Page Class.

请问上述这种说法对吗?如果对的,那为什么在CodeFile里定义的私有变量不能在aspx页面里访问呢?在asp.net 2.0里面,aspx和cs两个文件之间的关系是partial 还是Inherits?

问题补充: 存储于不同文件中的HTML:是指直接写在aspx页面里的程序.
RyanXM的主页 RyanXM | 初学一级 | 园豆:65
提问于:2008-09-22 17:22
< >
分享
最佳答案
0

比如Default.aspx页面,.cs可能生成一个叫_Default的类或者叫Default的类

而.aspx会生成一个叫aspnet_Default的类,这个类继承自_Default类/Default类

因此,如果你在.aspx写了与.cs中有相同签名的方法,.aspx中的方法是最后会执行的,.cs的就被无视了

Gray Zhang | 专家六级 |园豆:17610 | 2008-09-22 21:41
其他回答(11)
0

partial是用在类,接口的拆分定义上的,并不包括html

aspx是页面文件,cs是代码文件,不一样的,是分离的.

zjy | 园豆:3194 (老鸟四级) | 2008-09-22 17:32
0

partial是对cs中代码的拆分,和aspx没关系的

生鱼片 | 园豆:5757 (大侠五级) | 2008-09-22 17:47
0

好像是继承的关系,呵呵

丁学 | 园豆:18530 (专家六级) | 2008-09-22 18:01
0

aspx页面是继承cs代码的。他们是继承关系。aspx页面中这个地方可以看出来Inherits="类名"。

私有变量是不能访问的,应该使用protected或者public类型才可以。

它们的执行顺序就是先执行cs代码,后执行aspx代码页面。

侯垒 | 园豆:3435 (老鸟四级) | 2008-09-22 18:23
0

.aspx中定义的类是.aspx.cs中定义的类的子类

最直接的体现就是可以在.aspx中访问对应的.aspx.cs中定义的protected方法,而不能访问private方法。

JimLiu | 园豆:300 (菜鸟二级) | 2008-09-22 22:39
0

aspx是页面文件,cs是代码文件.

孤星赏月 | 园豆:125 (初学一级) | 2008-09-22 22:52
0

汇总下楼上各位的回答:   *_*

1.partial意指C#类,接口的折分,并不包含HTML文件.

2.Inherit指aspx的页面的C#代码和code behind所指CS文件的代码的继承关系.所以说aspx文件关联的CS文件的私有成员是不能访问的。如需要访问只能将来code behind文件的方法||属性的声明更改为protect,public.

3.partial 用在aspx的code behind文件中是来辅助原有的code behind方式这种方式叫做code beside.可以在在1.0中每往aspx上拖一个控件,那么在它的code behide 的文件就会有一个声明,而现在这个声明这被移到了别个文件中它的类名和code behide文件一样,且也有partial声明叫xxx.aspx.desinger.cs.

加冰的可乐 | 园豆:410 (菜鸟二级) | 2008-09-23 08:05
0

同意 zaluao 的说法

itman020 | 园豆:0 (初学一级) | 2008-09-23 16:55
0

如果你想研究的更深一点,可以参考它的内部机制:

关于编译和临时文件的:

The page handler factory is responsible for either finding the assembly that contains the page class or
dynamically creating an ad hoc assembly. The source code for the class is created by parsing the
source code of the requested .aspx resource, and it's temporarily saved in the following ASP.NET
temporary folder:
%SystemRoot%\Microsoft.NET\Framework\[version]\Temporary ASP.NET Files
In ASP.NET 1.x, the page class inherits from the code-behind class you created with Visual Studio. In
ASP.NET 2.0, the page class is an enhanced and completed version of the partial class in the Visual
Studio .NET code file. In particular, the partial class in the project is completed with a second partial
class dynamically arranged by the ASP.NET HTTP runtime.

 

关于部分类的职责:

Partial classes are a new feature of .NET Framework 2.0 compilers that allow you to break up the
definition of a class into multiple, partial files. Each constituent class appears to be a perfectly valid and
legal class definition, except that each is missing some of the logic and features expected of the overall
class. The compiler will then take care of merging partial classes into a regular, nonpartial class that
can be compiled to an assembly.
Partial classes are a source-level and assembly-limited way to extend the behavior of a class. In this
regard, partial classes are not an object-oriented feature. Partial classes save time during the
development cycle. They enable multiple teams to work on the same component at the same time, and
they provide an elegant way to add functionality to a class incrementally by adding a new class instead
of touching existing file classes.
Partial classes are used in ASP.NET 2.0 to overcome the brittleness of tool-generated code such as that
found in ASP.NET 1.x projects. If you ever developed applications with Visual Studio .NET 2003, you
should be familiar with those weird, auto-generated, semihidden regions of code that Visual
Studio .NET 2003 inserts to support page designers. That code is required to extend code-behind
classes with members that link to server controls dropped onto the underlying Web Form. Keeping the
code-behind class in sync with the evolution of the Web Form might not be easy if you tend to edit the
aspx source directly from the HTML editor. Partial classes remove the issue at the root: no more toolgenerated
code during the development phase. At compile time, the partial code-behind class is
completed with member definitions, and this code is completely hidden from developers.
ASP.NET Temporary Files
Generating an assembly for a particular .aspx resource is a two-step process. First, a class file is
created that fully represents the markup of the .aspx file. Second, the dynamically generated class is
compiled into an assembly and cached in the Temporary ASP.NET Files folder. There will be a distinct
folder for each installed version of ASP.NET

LanceZhang | 园豆:857 (小虾三级) | 2008-09-23 21:39
0

呵呵,回答这么多了,简单的说,就是code behind

李永京 | 园豆:3114 (老鸟四级) | 2008-09-23 23:19
0

呵呵,都差不多了

AndyFish | 园豆:1575 (小虾三级) | 2008-09-24 10:40
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册