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

2

17

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:

controls/C1.ascx
controls/C2.ascx

The controls have been registered in web.config:

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

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?

Bombacaceous answered 15/8, 2011 at 12:47 Comment(3)
possible duplicate of Use user control in the same folder as the pageOtten
Is there a reason why <%@ Register ... %> is considered "unclean"? Seems no different than having using statements at the top of a .cs file.Zagreus
It's only considered unclean when the control is already registered in web.config for every page or web user control, which is my case.Bombacaceous
N
26

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.

Neomineomycin answered 15/8, 2011 at 13:24 Comment(1)
Re-registering is the best option for me, I don't want to move the user controls into another subfolder just because ASP.NET acts strangely sometimes. Thanks.Bombacaceous
O
6

You could also put the controls into different folders. But I don't think this is much cleaner or better.

BTW: this behavior is by design, as you can read on this MSDN page (look for the yellow note almost at the end of the page).

Otten answered 15/8, 2011 at 13:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.