Nesting ASP.NET User Controls

While trying to nest ASP.NET user controls inside other user controls you may experience the following error:

The page ‘[control 1].ascx’ cannot use the user control ‘[control 2]‘, because it is registered in web.config and lives in the same directory as the page.

Basically there is a restriction that stops you nesting controls that exist in the same folder. The simple solution is to create subfolders for each control that you want to nest inside other controls.

So when displaying a website category that has subcategories you could create a user control to display the category and a separate user control to display a list of categories. The categories user control can then be added to the category user control.

To prevent the nested control error the user controls need to be created in separate folders:

  • ~/Controls/Category/Show/Show.ascx – display the category.
  • ~/Controls/Category/List/List.ascx – list categories.

 

This entry was posted in Programming and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

*
*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

* = Required