base class cast exception

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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,
 
Back
Top