[Mono-dev] UserControl type cast issue

Rafael Teixeira monoman at gmail.com
Mon Mar 13 18:19:29 EST 2006

Did you put the directive on the ascx, to have it inherit from your
class MyControl?
If not the created control descend from Control and can't be cast to a

Remember that the source for a new class is created from the xml/html
in the ascx/aspx file in ASP.NET 1.x. That class is compiled and is an
instance of it that is running in your page. If you don't instruct the
parser to generate a source with the right inheritance it doesn't have
a way to know it.


On 3/13/06, Slava Petrenko <slava at z.org.ua> wrote:
> Hi all,
> I have such a piece of code:
> class MyControl : UserControl
> {
>    .........
> }
> class MyPage : Page
> {
>     private void Page_Load(...)
>     {
>        MyControl control = ( MyControl ) LoadControl( "mycontrol.ascx" );
>     }
> }
> At line MyControl control = ( MyControl ) LoadControl( "mycontrol.ascx" );
> I get an error "Cannot cast from source type to destination type"
> I refered to MSDN - such a conversion is correct (they got a simulair
> sample to the code above), any suggestion?
> Slava.
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list

Rafael "Monoman" Teixeira
As I'm currently working a lot with Java and even fixing Java VMs
(JamVM/Kaffe) and GNU Classpath code, I think I may partly borrow the
title (Javaman) from my friend Bruno Souza and become the
MonoNJavaMan. Yeah, I may currently be crazier than usual...

More information about the Mono-devel-list mailing list