The difference between an ASP.NET page and a classic ASP page is that in ASP.NET the entire file's contents is parsed into a class definition and then complied into an assembly.ASP.NET 1.0 introduced a mechanism for separating programmatic logic from static page layout called codebehind. This technique involves creating an intermediate base class that sits between the Page base class and the machine-generated class from the .aspx file. The intermediate base class derives directly from Page, and class generated from the .aspx file derives from the intermediate base class instead of directly from from Page. With this technique, you can add fields, methods, and event handlers in your codebehind class and have these features inherited by the class created from the .aspx file, eliminating the need to sprinkle code throught the .aspx file.In ASP.NET 2.0 codebehind model, ASP.NET now will also generate a sibling partial class for your codebehind class that contains protected control member variable declarations. Your class is then complied together with this generated class definition, merged together, and then it becomes the base class for the class generated for the .aspx file.
而private则表示只能在类的内部使用
楼主的这句话里有错,它们不是“都是在一个页面中”
假设你有一个Default.aspx和Default.aspx.cs
那么,被编译后的实际情况是:
Default.aspx变成一个类名为Default_aspx(除非你在page指令中指定了ClassName)的类,在ASP命名空间下,它继承了Default.aspx的page指令中Inherits后的那个类(默认为_Default),你打开Default.aspx.cs后会发现,这个_Default类定义在里面,它继承了System.Web.UI.Page类所以说,如果你在代码分离文件中把方法定为了private,则只能在这个分离文件中相应的类中调用。
This technique involves creating an intermediate base class that sits between the Page base class and the machine-generated class from the .aspx file.
The intermediate base class derives directly from Page, and class generated from the .aspx file derives from the intermediate base class instead of directly from from Page.
With this technique, you can add fields, methods, and event handlers in your codebehind class and have these features inherited by the class created from the .aspx file, eliminating the need to sprinkle code throught the .aspx file.In ASP.NET 2.0 codebehind model, ASP.NET now will also generate a sibling partial class for your codebehind class that contains protected control member variable declarations. Your class is then complied together with this generated class definition, merged together, and then it becomes the base class for the class generated for the .aspx file.
页面和后台类是继承的关系
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>