加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

ASP.NET CodeFileBaseClass属性与从System.Web.UI.Page继承

发布时间:2020-12-16 06:21:58 所属栏目:asp.Net 来源:网络整理
导读:我刚从System.Web.UI.Page继承为我的页面创建了一个基类: public abstract class PageBase : System.Web.UI.Page{ ...} 当我注意到你也可以在ASP.NET视图中声明一个基页: %@ Page Language="C#" CodeFileBaseClass="PageBase.cs" CodeFile="page.aspx.cs"
我刚从System.Web.UI.Page继承为我的页面创建了一个基类:

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

当我注意到你也可以在ASP.NET视图中声明一个基页:

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

有人可以解释一种方法的优点和缺点是什么吗?你什么时候使用一个,或者它们都是一样的?如果你同时使用两者会怎么样?

解决方法

CodeFileBlassClass,CodeFile,继承与继承一起工作,而不是继承.

例如,指定CodeFile =“page.aspx.cs”而不存在page.aspx.cs将导致:

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

假设存在page.aspx.cs,则指定CodeFileBaseClass =“PageBase.cs”而不存在PageBase.cs将导致:

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

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

引用Microsoft’s @Page MSDN Documentation:

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读