locked
Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl) RRS feed

  • Question

  • User1506489892 posted

    Here is the page directive for the page that throws the error:

    <%
    @ Page language="c#" Inherits="University.AspNet.Index" CodeFile="Index.aspx.cs" %>

    The code behind file Index.aspx.cs starts like this:

    using University.Framework.UI;

    namespace University.AspNet

    {
          
    public partial class Index : University.Framework.UI.HomePage

    The code behind is extending this HomePage class, which is in turn an extenstion of another class University.Framework.UI.Page, which extends the System.Web.UI.Page class.

    Am I misusing the Inherits parameter somehow?

    Oddly there is a blue squiggle under only the first 'i' in University in the code behind that links to the error in the subject of this post.

    Any insights as to the nature of this error would be greatly appreciated. Thanks.

    Tuesday, November 29, 2005 1:23 PM

Answers

  • User159848876 posted
    It looks like you are missing the CodeFileBaseClass attribute, which should point to your baseclass.

    <%@ Page language="c#" Inherits="University.AspNet.Index" CodeFile="Index.aspx.cs" CodeFileBaseClass="XXXXXX" %>

    CodeFileBaseClass

    Specifies a path to 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 the 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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 30, 2005 12:56 PM

All replies

  • User159848876 posted
    It looks like you are missing the CodeFileBaseClass attribute, which should point to your baseclass.

    <%@ Page language="c#" Inherits="University.AspNet.Index" CodeFile="Index.aspx.cs" CodeFileBaseClass="XXXXXX" %>

    CodeFileBaseClass

    Specifies a path to 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 the 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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 30, 2005 12:56 PM
  • User-1001225323 posted
    Actually I had this error and it occurred because I tried adding the class to a "namespace", try removing the name space.
    Saturday, March 3, 2007 8:54 PM
  • User-2143514933 posted

    Changing the attribute from "codefile" to "codebehind", should do the trick.

     I found the related article here. http://tek-wise.blogspot.com/2009/07/aspnet-make-sure-that-class-defined-in.html

    Thursday, November 27, 2008 10:42 AM
  • User203683542 posted

    There is no need to remove the namespace. In your <%page %> tag, change the Inherits attribute to point to the full class name instead of just the class name.

    That is, if a code-behind class "_Default" is in the namespace MyNameSpace, then change Inherits = "MyNameSpace._Default" from Inherits = "_Default"

    Wednesday, May 6, 2009 2:34 AM
  • User822211944 posted

    I forgot to inherit System.Web.UI.Page in the basepage class....

     

    I put BasePage.cs in the App_Code folder which contains the implemented class BasePage.  I then inherited the BasePage class in MyClass like this:

     public partial class MyClass : BasePage { //define class}

     

    and then in the BasePage.cs make sure the class BasePage inherits from the System.Web.UI.Page class here.. if not.. make sure you add it like this...

    public class BasePage : System.Web.UI.Page{ //define basepage class here}

     

    Wednesday, May 27, 2009 11:41 AM
  • User-1408383757 posted
    1. Namespace X
          Namespace Y
            Namespace Z            
                Public Class BasePage
                    Inherits System.Web.UI.Page
     
     2. Public Class MainPageClass 
        Inherits X.Y.Z.BasePage
     
    3. <%@ Page AutoEventWireup="false" CodeFile="myPage.aspx.vb" Inherits="myPage" %>
        Code-behind:
              Partial Public Class myPage
                  Inherits MainPageClass 
     ----------------------------------------
    It's working fine locally in VWD-2008 but it's throwing an exception when I run the same app in WSS(sharepoint).
    Exception: error ASPNET: Make sure that the class defined in this code file matches the 'inherits' attribute, 
    and that it extends the correct base class (e.g. Page or UserControl).   at System.Web.Compilation.AssemblyBuilder.Compile() 
     
    Any idea, where I am doing wrong! 
    Thanks

     

    Wednesday, September 16, 2009 4:31 PM
  • User-1145340147 posted

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="public_home.aspx.cs" Inherits="public_home" %>

    plz help...

    i'm trying to alter a website code wich hav no name spaces

    im getting error

    my code is this...


    ..

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="public_home.aspx.cs" Inherits="public_home" %>


    Tuesday, August 17, 2010 4:09 PM
  • User1834370702 posted

    Despite the age of the thread, I just wanted to report for the record that I ran into this issue when I had a stupid typo like what follows in an .aspx page:

    <% if( SomeString.Length > 0 ) %>
        <blockquote><%= SomeString %></blockquote>
    <% } %>

    The above-documented error message was what was reported, being very nonobvious. It was just caused by the missing "{" in the if block. The correct if line should have been:

    <% if( SomeString.Length > 0 ) { /* note the open brace ! */ %>

    Something about the missing/unmatched brace structure must mess up the whole compilation of a partial class. Aggravatingly, the nonsense error message made troubleshooting this take an hour longer than it should have. Grr.

    Tuesday, June 14, 2011 11:03 PM