【问题标题】:Why doesn't ASP.Net recognize my user control?为什么 ASP.Net 不能识别我的用户控件?
【发布时间】:2011-03-23 21:47:00
【问题描述】:

在我当前的项目(ASP.Net 2.0)中,我有很多网络用户控件,其中包括“Office.ascx”。

Office.ascx 使用这一行加载到另一个控件中:

guide_controls_display_Office o = (guide_controls_display_Office)LoadControl("Office.ascx");

我正在加载它的控件已使用此行注册了 Office.ascx:

<%@ Register TagName="office" TagPrefix="uc" Src="Office.ascx" %>

但我在启动网页时收到此错误:

Unable to cast object of type 'ASP.guide_controls_display_office_ascx' to type 'guide_controls_display_Office'.

为什么我会收到此消息?我可以看到第一种类型有“ASP”。在它面前。这正常吗?

【问题讨论】:

    标签: .net asp.net


    【解决方案1】:

    这似乎是一个已知问题。我不得不清除临时文件并重建项目以使错误消息消失,“暂时”。

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

    最终的解决方案似乎是接口。使用您需要的方法构建一个接口或抽象类,让您的用户控件实现该接口,然后使用接口/抽象类加载该控件:

    Control c = LoadControl("~/guide/controls/display/Office.ascx");
    AOffice io = c as AOffice;
    io.ID = "office_" + i.ToString();
    offices.Controls.Add(io);
    
    io.DisplayEdit = displayedit;
    

    【讨论】:

    • 但是对于这个非常烦人的问题是否有永久的解决方案?
    猜你喜欢
    • 1970-01-01
    • 2020-11-25
    • 2010-10-19
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-21
    相关资源
    最近更新 更多