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?

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
michalstanko
  • 579
  • 5
  • 19
  • possible duplicate of [Use user control in the same folder as the page](http://stackoverflow.com/questions/2964337/use-user-control-in-the-same-folder-as-the-page) – M4N Aug 15 '11 at 13:32
  • Is there a reason why <%@ Register ... %> is considered "unclean"? Seems no different than having using statements at the top of a .cs file. – mikemanne Aug 15 '11 at 13:47
  • 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. – michalstanko Aug 19 '11 at 10:40

2 Answers2

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.

Jon Adams
  • 24,464
  • 18
  • 82
  • 120
  • 5
    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. – michalstanko Aug 17 '11 at 09:18
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).

M4N
  • 94,805
  • 45
  • 217
  • 260