ASP.NET CodeFileBaseClass属性与从System.Web.UI.Page继承 [英] ASP.NET CodeFileBaseClass attribute vs. inherit from System.Web.UI.Page

查看:88
本文介绍了ASP.NET CodeFileBaseClass属性与从System.Web.UI.Page继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚通过继承System.Web.UI.Page为页面创建了基类:

I've just created a base class for my pages by inheriting from System.Web.UI.Page:

public abstract class PageBase : System.Web.UI.Page
{
    ...
}

当我注意到您还可以在ASP.NET视图中声明一个基础页时:

When I noticed that you can also declare a base page in an ASP.NET view:

<%@ Page Language="C#" CodeFileBaseClass="PageBase.cs" CodeFile="page.aspx.cs"
    Inherits="page" %>

有人可以解释这两种方法的优缺点吗?您什么时候可以使用另一个,还是两个都一样?如果您同时使用两者会发生什么?

Can someone explain what the pros and cons of either method are? When would you use one over the other, or are they both the same? What happens if you used both at the same time?

推荐答案

CodeFileBaseClassCodeFileInherits与继承一起工作,而不是代替继承.

CodeFileBaseClass, CodeFile, Inherits work together with inheritance, not in place of inheritance.

例如,指定不存在page.aspx.csCodeFile="page.aspx.cs"将导致:

For example, specifying CodeFile="page.aspx.cs" without page.aspx.cs existing will result in:

Parser Error Message: The file '/page.aspx.cs' does not exist.

假设page.aspx.cs存在,指定不存在PageBase.csCodeFileBaseClass="PageBase.cs"将导致:

Assuming page.aspx.cs exists, specifying CodeFileBaseClass="PageBase.cs" without PageBase.cs existing will result in:

Parser Error Message: Could not load type 'PageBase.cs'.

另一方面,您可以从PageBase 继承而无需指定CodeFileBaseClass属性.但是,当从基类中引用页面上的控件时,这可能会导致意外的行为.

On the other hand you may inherit from PageBase without specifying the CodeFileBaseClass attribute. This however could result in possible unexpected behaviour when referencing controls on the page from the base class.

引用 Microsoft的@Page MSDN文档:

CodeFileBaseClass
指定页面的基类的类型名称及其关联的代码隐藏类.该属性是可选的,但是当使用该属性时, CodeFile 属性也必须存在.当您要实现共享方案(在其中定义通用)时,请使用此属性 基类中的字段(以及可选的相关事件) 参考网页中声明的控件.由于ASP.NET 代码生成模型(如果您在基类中定义了字段) 在不使用此属性的情况下,在编译时新的成员定义 将为网页中声明的控件生成(在 单独的局部类存根),并且您所需的方案不会 工作.但是,如果您使用 CodeFileBaseClass 属性进行关联 页面的基类,然后您就可以创建局部类(它的 名称被分配给 Inherits 属性,其源文件为 由 CodeFile 属性引用)从基类继承, 那么基类中的字段将能够引用 生成代码后页面上的控件.

CodeFileBaseClass
Specifies the type name of a base class for a page and its associated code-behind class. This attribute is optional, but when it is used the CodeFile attribute must also be present. Use this attribute when you want to implement a shared scenario, where you define common fields (and optionally, associated events) in a base class to reference the controls declared in a Web page. Because of the ASP.NET code generation model, if you defined the fields in a base class without using this attribute, at compile time new member definitions would be generated for the controls declared in the Web page (within a separate partial class stub), and your desired scenario would not work. But if you use the CodeFileBaseClass attribute to associate the base class with the page, and you make your partial class (its name is assigned to the Inherits attribute and its source file is referenced by the CodeFile attribute) inherit from the base class, then the fields in the base class will be able to reference the controls on the page after code generation.

这篇关于ASP.NET CodeFileBaseClass属性与从System.Web.UI.Page继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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