开发者

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:

controls/C1.ascx
controls/C2.ascx

The controls have been registered in web.config:

<configuration>
    <system.web>
        <pages>
            <controls>
               开发者_JS百科 <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?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜