No Gravatar

I’m writing a few ASP.NET pages in C# and I ran into a little snag. I have a file called profile.aspx. I’m using the CodeBehind technique to separate my presentation from my business logic. The @Page directive of the file has

<%@ Page Language="C#" MasterPageFile="~/admin/AdminMasterPage.master" AutoEventWireup="true" CodeFile="profile.aspx.cs" Inherits="profile" Title="Untitled Page" %>

As you can see from reading that, I have a business logic file called profile.aspx.cs and the class name is ‘profile’. A little more background: I’m also using a custom base page to alter the behavior of all pages in my site (e.g., to require some sort of authentication before allowing a user to move through the application). That means that the class declaration in profile.aspx.cs looks like this

// import stuff...
namespace MyNamespace
{
public partial class profile : BaseAdminPage
{
protected void Page_Load(object sender, EventArgs e)
{
// Constructor code...
}
}
}

If you don’t understand what the code means (and you care) then start comments and clarification will ensue. When I tried to compile with Visual C# Express 2005 I got this error:

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). C:\Documents and Settings\Jed\My Documents\Visual Studio 2005\WebSites\CollectionZone\admin\profile.aspx.cs 13 33 C:\...\CollectionZone\

Yeah, I didn’t understand that at first either. When I searched our favorite engine I got a bunch of guys saying crap like “It worked fine until I put in the namespace. It’s the namespace that broke it. So I took out the namespace and it worked again.” Those guys aren’t very analytical and they certainly don’t understand the very basic concepts of a namespace. I won’t go into namespaces here (maybe in comments if someone’s interested) but the issue is in the Inherits="profile" found in the profile.aspx @Page directive. All I needed to do was correctly state which class the .aspx page was inheriting and WHAM! all was right in the world. I had to have Inherits="MyNamespace.profile" because a class called profile and a class called MyNamespace.profile are different classes (there is your basic premise of namespaces).


3 Responses to “Class matching with ASP.NET code-behind”  

  1. 1 Mustafa Al-KaouriNo Gravatar

    Thanks, your solution has solved my code problem.

  2. 2 JedNo Gravatar

    I’m glad it helped.

  3. 3 Ankit BhushanNo Gravatar

    thanks it helped my code also

Leave a Reply


 

cyberpir8.net is using WP-Gravatar