tags:

views:

60

answers:

1

In my current project (ASP.Net 2.0) I have a lot of web user controls, among them "Office.ascx".

Office.ascx is loaded into another controls using this line:

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

The control I am loading it into has Office.ascx registered using this line:

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

But I get this error upon launching the web page:

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

Why do I get this message? I can see that the first type has "ASP." in front of it. Is that normal?

+1  A: 

It seems that this is a known problem. I had to clear temporary files and rebuild the project to make the error message go away, "for a while".

http://stackoverflow.com/questions/512064/unable-to-cast-object-of-type-x-to-type-x-asp-net

The final solution seems to be interfaces. Build an interface or an abstract class with the methods you need, make your user control implement the interface, and then load the control using the interface/abstract class:

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;
Erlend D.
+1 for posting solution to this
citronas
But is there a permanent soulution to this very annoying problem?
Erlend D.
There is. I updated the answer.
Erlend D.