简体   繁体   中英

Convert UserControl To My Custom Control

I have class that Inherit from UserControl Class.

public class Module : UserControl
{
    // Custom Property And Method
}

How can i load a UserControl (.ascx) and convert to my Module Class?

UPDATE :

I try all suggest but in best case I get null. the error is : Unable to cast object of type 'ASP.UC_acsx' to type'BICT.Module'

my class is like this :

namespace BICT
{
    public class Module : UserControl
    {
        public Module()
        {
          // Some Initial
        }
        // and some extra property exp.
        public int Index{ get; set;} 
    }
 }

is somthing wron with my code?

You can't, by design.

Module is a derivative class of UserControl . Therefor all Module classes can be casted to UserControl , but UserControl classes can't be casted to Module (see Footnote) . Read up in inheritence.

Footnote: Yes, of course you can cast UserControl to Module , but only if it is a Module in the first place, and is just loaded with the lower type.

怎么样使用

(Module)Page.LoadControl("Module.ascx")

You can try this code:

Module module = LoadControl("Module.ascx") as Module;
if(module != null)
{
    //it is an instance module
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM