我有 3 个 .NET 项目。
其中一个项目是名为 Index 的 ASP.Net Web 表单应用程序。下面列出了其他项目索引。数据库项目是数据库层索引。平台项目是业务层。
在业务层即时加载用户控件。在 db 中有关于这些 UserControls 的信息。(路径、ascx 文件、名称、标题、内容、位置等)
在业务层中,我创建了一个从名为 ModuleControl 的 UserControl 驱动的类。
System.Web.dll 引用的 Index.Platform 也使用 Using System.Web.UI.WebControls。
我打算在我加载的 UserControls 中使用这个 ModuleControl 字段。在 Default.aspx 的 Load_Page 事件上实例化了另一个名为 IndexSite 的类。
namespace Index.Platform.Modules
{
public class ModuleControl : System.Web.UI.UserControl
{
public string Title { get; set; }
public bool ShowTitle { get; set; }
public string Content { get; set; }
public ModuleControl()
{
}
}
}
//Index.Platform.IndexSite
private void InitializeModules(System.Web.UI.Page page)
{
string mPath = null;
try
{
ModuleDictionaryList = DBFactory.LoadModules();
PositionList = DBFactory.PositionList;
ModuleList = DBFactory.ModuleList;
foreach (KeyValuePair<string, List<module>> pair in ModuleDictionaryList)
{
foreach (var item in pair.Value)
{
mPath = "/Modules/" + item.Name + "/" + item.Name + ".ascx";
iControl = (ModuleControl)page.LoadControl(mPath);
ShowTitle = Convert.ToBoolean(item.ShowTitle);
iControl.ShowTitle = ShowTitle;
iControl.Title = item.Title;
iControl.Content = item.Content;
panel = (PlaceHolder)page.Master.FindControl(item.Position);
panel.Controls.Add(iControl);
//HttpContext.Current.Response.Write(item.Position + "<br>");
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
this class takes an argument from Default.aspx. Page sends its self.
protected void Page_Load(object sender, EventArgs e)
{
IndexSite site = IndexSite.Instance.Create(this);
}
是的,在 Bussines 层中,我使用 LoadControl 方法加载 USerControl,我还将这些控件添加到 MasterPage(PlaceHolder)中的面板控件中。
我的问题出在这一行: iControl = (ModuleControl)page.LoadControl(mPath);
我不能将 UserControl 转换为 ModuleControl。记住这个 ModuleControl 是从 UserControl 驱动的,而我所有的 ascx 文件都是从 ModuleControl 类驱动的。
引发此错误:“无法将 'ASP.modules_mod_login_mod_login_ascx' 类型的对象转换为 'Index.Platform.Modules.ModuleControl'。”
如果我在主应用程序中执行这些过程,则转换 ModuleControl 没有错误。
当 id 将我的应用程序分隔为 3 时。我卡在这里。