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
{
publi开发者_JS百科c 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.
You can try this code:
Module module = LoadControl("Module.ascx") as Module;
if(module != null)
{
//it is an instance module
}
How about using
(Module)Page.LoadControl("Module.ascx")
精彩评论