Use user control in the same folder as the page
Asked Answered
D

4

21

I get this message at runtime of ASP.NET 2 page :

The page 'MyFolder/blabla.aspx' cannot use the user control 'MyFolder/MyControl.ascx', because it is registered in web.config and lives in the same directory as the page.

Of course I can separate them to 2 different folders and thus solve the problem, but the question is :

WTF !?!?! Why I can't put them in the same folder ?! Why can't they all .. get along !?! :)

Thanks

Demos answered 3/6, 2010 at 8:31 Comment(1)
Funny that I get this same exact error for a control I have which includes another control. It even says "The page 'MyCONTROL.aspc'...". Page != Control. So I guess having more than one Control in a single directory is a problem.Maxillary
O
13

This limitation is by design due to an internal design consideration re: performance.

See here for further info.

Remarks

The TagPrefixInfo class allows you to programmatically access and modify tag-prefix information stored in a configuration file. It provides the same functionality as the ASP.NET @Register directive. Tag prefixes associate a "namespace" in ASP.NET to the assemblies and namespaces that must be included for custom controls and user controls to work properly. TagPrefixInfo objects are stored as members of a TagPrefixCollection object. The TagPrefixCollection class allows you to programmatically access and modify the controls subsection of the pages section of a configuration file.

TagPrefixInfo objects are added to the collection using the add element and specifying a value for the tagPrefix attribute along with values for other relevant attributes. The other required information varies based on the kind of control you will use with the specified tag prefix:

  • If it is a user control, you must define the TagPrefix, TagName, and Source properties.
  • If it is a custom control, you must define the TagPrefix, Namespace, and Assembly properties. The Assembly property is not required if the control is in the application code directory. The same tagPrefix value can be used to map to multiple assemblies or namespaces.

Note When a source is specified, the user control itself must not be in the same directory as the page. If it is, you get a run-time error when you attempt to load the page.

Ordonnance answered 3/6, 2010 at 8:41 Comment(2)
Can you elaborate on the performance point? I'm just curious.Wiesbaden
@MichaelCrenshaw I suspect it's something to do with how ASP.NET WebForms compiles entire directories at once (so each directory of .aspx/ascx/master files gets its own output assembly in the Temporary ASP.NET Files directory.Heuser
N
8

If you register it in the page or user control instead of the web.config it will load properly. Add the following to the top of the page.

<%@ Register TagPrefix="MyControlTagPrefix" TagName="MyControlTagName" Src="~/MyFolder/MyControl.ascx" %>
Neuter answered 15/11, 2013 at 20:16 Comment(0)
C
2

I suspect you could do it without registering it, if it was essential. You could probably have a PlaceHolder and then use .Controls.Add(LoadControl("path.ascx"))

But if it's not essential then put it in a different directory due to the reasons @Barry says.

Confection answered 3/6, 2010 at 12:0 Comment(0)
H
0

The answer can be found inside the .NET Framework Reference Source code for System.Web.dll:

https://referencesource.microsoft.com/#System.Web/UI/TagNameToTypeMapper.cs,ced01a48dbbb9f9c

TryUserControlRegisterDirectives

Don't allow a config registered user control to be used by a page that lives in the same directory, to avoid circular references. More specifically, this would break batching because our simple dependency parser would not be able to detect the implicit dependency triggered by the presence of a tag (VSWhidbey 165042).

Heuser answered 6/11, 2019 at 8:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.