ASP.NET error: The page Y.ascx cannot use the user control X.ascx


I am getting the error below when trying to build the web site project in Visual Studio 2010:

The page '/WebSite/controls/C2.ascx' cannot use the user control '/WebSite/controls/C1.ascx', because it is registered in web.config and lives in the same directory as the page.

I have 2 web user controls:


The controls have been registered in web.config:

                <add src="~/controls/C1.ascx" tagPrefix="my" tagName="C1"/>
                <add src="~/controls/C2.ascx" tagPrefix="my" tagName="C2"/>

C1.ascx contains just a static HTML, C2.ascx is trying to include C1:

C1.ascx contains just some plain static simple HTML. C2.ascx is trying to include C1.ascx:

<%@ Control Language="VB" %>
<my:C1 runat="server" />
<p>Hello from C2</p>

When trying to build the project, I am getting the error message at the top. I realise this issue can be fixed by adding another Register directive to C2.ascx…:

<%@ Register Src="~/controls/C1.ascx" TagPrefix="ctl" TagName="C1" %>

…but I'm wondering if there's a cleaner solution and why am I getting the error in the first place?

Best Solution

Your only possible solutions are to:

  • Move the control out of the directory its currently sharing with outer.ascx, or
  • Re-register the control inside of the outer.ascx like you already mentioned
  • Re-write them in code as controls in a separate library

I personally think moving is the easiest, if it will work for your solutions. Second would be re-registering, even though annoying. Abstracting them out into a full code library is probably not worth the effort if this is the only reason you are doing it.