Error with code-behind for user control

A

Alan Silver

Hello,

I have a set of user controls that all use the same code-behind file.
The first line of each of the ascx files looks like...

<%@ CodeFile="ShowCategory.cs" Language="C#" Inherits="ShowCategory" %>

and the top of the code-behind (ignoring the "using" statements) looks
like...

namespace MyECommClasses {
public partial class ShowCategory : UserControl {

However, when I try to run this site, VWD gives me the following error
in the code-behind file...

"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)."

As you can see, the inherits class matches the one in the class file, so
it seems that it thinks that I'm not extending the right base class.
However, one of the ones it suggests is "UserControl" which is what I do
extend.

Any ideas? TIA
 
A

Alan Silver

Alan Silver said:
Any ideas? TIA

Well, after posting, I found the answer (as usual!!). It was the
namespace that was causing the problem. I had added that in to try and
solve an earlier problem!! Removing it fixed the error I was getting.

Unfortunately, I'm now getting a different error. The line shown
below...

ctlShowCategory = (ShowCategory)LoadControl(strControlName);

....throws an exception...

"Unable to cast object of type 'ASP.ShowCategory' to type
'ShowCategory'."

The top of the ascx file now looks like...

<%@ CodeFile="ShowCategory.cs" Language="C#" Inherits="ShowCategory"
ClassName="ShowCategory" %>

and the class definition in the code behind looks like...

public partial class ShowCategory : UserControl {

so I don't see why it can't cast it. Any ideas? TIA
 
A

Alan Silver

Alan Silver said:
Any ideas? TIA

This is getting to be a bit like a monologue!!

Anyway, I thought I would let you know that the answer turned out to be
nothing more complex than stopping the server and deleting the temporary
ASP.NET files. It seems that the server got itself confused.

Hope this helps someone.
 
Joined
Nov 27, 2008
Messages
1
Reaction score
0
I found the solution here:

codeguru302.blogspot.com

Changing the attribute from "codefile" to "codebehind" worked for me.
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top