base class cast exception

G

Guest

Hi all,

I have created a new class (MyCustomUserControl) which is based on
UserControl.
I create an instance of my class using the next line of code:

MyCustomUserControl mycontrol = (MyCustomUserControl) ((UserControl)
Page.LoadControl("pathtoanascx.ascx"));

This line of code, gives me an InvallidCastException. (The cast from
TemplateControl to UserControl is no problem)

My class and its constructor is defined as followed:

public class MyCustomUserControl : UserControl
{
public MyCustomUserControl () : base()
{
} // Constructor
}

I thought this might not be a problem. What am I missing?

Thanks in advance,

Michel
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Did you tried:

MyCustomUserControl control =
(MyCustomUserControl )LoadControl("pathtoanascx.ascx");

If you still get error, do like this:
object o = (MyCustomUserControl )LoadControl("pathtoanascx.ascx");

and put a breakpoint in the next line, do a quickwatch of "o" and it will
tell you the type of the control.

Cheers,
 

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

Top