gpt4 book ai didi

c# - 无法将类型为 'ASP.MyControlName_ascx' 的对象转换为类型 'MyControlName'

转载 作者:太空宇宙 更新时间:2023-11-03 15:04:46 25 4
gpt4 key购买 nike

我有一个使用 asp.net 构建的网站。

在我的本地 PC 上工作正常。但是当我在服务器上部署它之后,当我尝试转到页面时收到以下错误消息

--Data--System.Collections.ListDictionaryInternal--Base Exception--System.InvalidCastException: Unable to cast object of type 'ASP.uc_eventmanagment_skill_skillbind_ascx' to type 'HRMS.uc.Skill.SkillBind'.
at HRMS.Task.LoadAdminPanels(String page)
at HRMS.Task.Page_Load(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)--Inner Exception--System.InvalidCastException: Unable to cast object of type 'ASP.uc_eventmanagment_skill_skillbind_ascx' to type 'HRMS.uc.Skill.SkillBind'.
at HRMS.Task.LoadAdminPanels(String page)
at HRMS.Task.Page_Load(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)--Source--System.Web--StackTrace-- at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

加载我正在使用此代码的 ASCX 文件

            SkillBind SkillBind = (SkillBind)LoadControl("/uc/EventManagment/Skill/SkillBind.ascx");
SkillBind.ID = "SkillBind";
pHolderContainer.Controls.Add(SkillBind);

怎么了?

最佳答案

问题似乎源于在将用户控件添加到页面之前分配 ID 属性:

SkillBind.ID = "SkillBind";
pHolderContainer.Controls.Add(SkillBind);

回到过去使用 webforms 的时候,我发现在为它分配任何属性之前,应该将用户控件添加到 Control 继承的容器中,即通过交换第二行和第三行以使用户控件工作:

SkillBind SkillBind = (SkillBind)LoadControl("/uc/EventManagment/Skill/SkillBind.ascx");
pHolderContainer.Controls.Add(SkillBind); // assign user control first
SkillBind.ID = "SkillBind"; // then setting ID attribute

此外,不要忘记在包含 SkillBind 控件的 ASPX 页面上包含此引用指令行:

<%@ Reference Control="/uc/EventManagment/Skill/SkillBind.ascx" %>

然后在开发版本的 web.config 中添加此元素以清除此类错误(可选):

<compilation batch="false"/>

之后,尝试清理并重建项目。

相关问题:

Unable to cast object of type 'X' to type 'X' - ASP.NET

Unable to cast object of type 'ASP.MyControlName_ascx' to type 'MyControlName'.

关于c# - 无法将类型为 'ASP.MyControlName_ascx' 的对象转换为类型 'MyControlName',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44237919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com