Error when creating a webpart using user controls RRS feed

  • Question

  • I received the following error when trying to create a webpart with user controls. 

    Unable to cast object of type 'ASP._controltemplates_UserControl_ascx' to type 'namespace.class'. I deleted all the files from the location
    C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files, but still the same error occurs.
    Here is the code for referencing the user control. 

    protected const string defaultUserControlPathField = "~/_controltemplates/myUserControl.ascx";
     protected string userControlPathField = defaultUserControlPathField;
     namespace.class tempControl;
    UserControl myControl;
     myControl = (UserControl)this.Page.LoadControl(this.userControlPathField);
                        tempControl = (namespace.class)myControl;

    Does anyone have an idea about this error?


    Saturday, October 23, 2010 3:50 PM


  • I think the first thing you should do to debug this is find out the actual runtime type. You can tell in debug mode in the immediate window using
    ? myControl.GetType();

    I say this because your code should work. The most likely reason it doesn't is because your control is really something other than namespace.class.


    Saturday, October 23, 2010 10:16 PM