2

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 Answer 1

1

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".

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;
Sign up to request clarification or add additional context in comments.

1 Comment

But is there a permanent soulution to this very annoying problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.