I recently had an issue where I converted some User Controls to code-behind model, and moved them to another directory. I then found I couldn’t reference them in code. After a post on the ASP.NET forums, I came to a solution, and devised the rules as to how you can reference a User Control in code:

  1. The control needs to have a ClassName tag in the <%@ Control %> tag (actually, I think if it doesn’t you’ll use the generated class name?)
  2. The page has to use a <%@ Reference %> tag in the .aspx file to reference the control
  3. If page uses a code-behind file, the namespace “ASP” contains all controls reference with the <%@ Reference %> tag. If it’s inline code, there’s no need for this.

If your page is inline code, you don’t have to worry about the ASP namespace. The ASP namespace is a dynamic namespace that contains the controls you referenced with the <%@ Reference %> tag in the markup file.

I hope this helps anyone having problems referencing User Controls by classname in their code. I searched for quite a while and I only got answers like “don’t use code-behind if you want to do that” or “have all your controls implement a Interface and reference them by that interface”.

Please post any comments or questions. I check my email/comments daily, so a response should be pretty quick (usually about 1 business day maximum. If I’m at a computer working, I usually respond right away). You can also contact me by email.

, ,