Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将UserControl转换为我的自定义控件_C#_Asp.net - Fatal编程技术网

C# 将UserControl转换为我的自定义控件

C# 将UserControl转换为我的自定义控件,c#,asp.net,C#,Asp.net,我有一个从UserControl类继承的类 public class Module : UserControl { // Custom Property And Method } 如何加载UserControl.ascx并转换为我的模块类 更新: 我尝试所有建议,但在最好的情况下,我得到零。 错误是:无法将“ASP.UC_acsx”类型的对象强制转换为“BICT.Module”类型 我的课是这样的: namespace BICT { public class Module :

我有一个从UserControl类继承的类

public class Module : UserControl
{
    // Custom Property And Method
}
如何加载UserControl.ascx并转换为我的模块类

更新:

我尝试所有建议,但在最好的情况下,我得到零。 错误是:无法将“ASP.UC_acsx”类型的对象强制转换为“BICT.Module”类型

我的课是这样的:

namespace BICT
{
    public class Module : UserControl
    {
        public Module()
        {
          // Some Initial
        }
        // and some extra property exp.
        public int Index{ get; set;} 
    }
 }
我的代码有问题吗?

出于设计,你不能

模块是UserControl的派生类。因此,所有模块类都可以强制转换为UserControl,但UserControl类不能强制转换为模块(参见脚注)。阅读《继承》一书

脚注:是的,当然可以将UserControl强制转换为模块,但前提是它首先是一个模块,并且只加载了较低的类型。

使用

(Module)Page.LoadControl("Module.ascx")
您可以尝试以下代码:

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

这完全没有意义……我让我的usercontrol从模块继承,一切正常。