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
{
...
}
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?
CodeFileBaseClass,CodeFile,Inheritswork together with inheritance, not in place of inheritance.For example, specifying
CodeFile="page.aspx.cs"withoutpage.aspx.csexisting will result in:Assuming
page.aspx.csexists, specifyingCodeFileBaseClass="PageBase.cs"withoutPageBase.csexisting will result in:On the other hand you may inherit from
PageBasewithout specifying theCodeFileBaseClassattribute. This however could result in possible unexpected behaviour when referencing controls on the page from the base class.To quote from Microsoft’s @Page MSDN Documentation: