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.

, ,

7 comments untill now

  1. Hi Aziz,

    this is interesting post that you have made. I have similiar but little different problem with referencing user control and I’d like to ask you if you have encountered it and solved it. I have some user control and I’d like to use it in custom control. Custom control is just C# code – no markup, so I can’t use Reference tag as you did in your post. Is there any possibility to use user control in custom control? I am searching the web for the solution for quite a while now but without success. Thank you for reply in advance.

  2. Interesting question. I’m actually not sure if this is possible. Try importing the namespace “ASP” and see if the class is in there (if the UserControl has a ClassName tag in it’s @Control directive, it will be that, otherwise it will be the name of the partial class).

    I don’t know if that will work because the ASP namespace only contained Referenced classes in the examples I dealt with, but I wasn’t working with custom controls at all.

    Your last resort would be to create an interface for each UserControl you want to use, specifying all the public properties/methods you want to call. Then have the UserControl reference that in an @Implements directive (if I remember correctly). You can then load that UserControl and reference it as that interface. Make sure you’re using the LoadControl(string) method to initialize dynamic controls, and NOT calling the constructor.

    Let me know if you need more help or examples.

  3. Does the aspx page who adds the user control from the code behind need the @Reference tag? I’ve been reading that there are ways to register a user control in the web.config but have not been able to get this to work.

    Have you been able to find a way to centralize all of the user control references in the web.config so that they don’t have to be repeated in each aspx file that loads and adds the controls programmatically?

  4. I had previously tried the “add” tags in the web.config but still couldn’t reference the control from the code behind. I might try your suggestion of making it into an assembly and see how that works out.

  5. Yes, that might be the only way if you want to avoid the Reference tag in each page. Let me know how it turns out, and if it’s easy to maintain.

  6. I have done all the above steps as you mentioned. and it works fine on my development machine.
    But when I deploy it on my testing server. My report doesn’t display record, until I goto second page and then back to 1st page or click on any group.

    I tried so much but unable to find its solution do any one have face such an issue

  7. Waqar, are you referring to Crystal Reports 2008?

    On the testing server, delete the Virtual Directory “crystalreportviewers12″. This can be found under:


    Then, copy the “crystalreportviewers12″ folder from your install directory (something like “C:\Program Files\Business Objects\Common\4.0″) to that directory.

    (Before doing this, make sure you install all patches for CR2008).

    Let me know if that helps.

Add your comment now

CommentLuv Enabled