Error BC30002 usercontrol not defined

Tried to compile an (.net Framework 2.0) website using aspnet_compiler.exe in updateable mode and kept getting Error BC30002 ASP.usercontrols_uc_ascx is not defined..

This was particularly puzzling as when doing a right click build website inside visual studio the website built without errors, and when running the website on the development machine's IIS it also ran without incident. Therefore, the issue is to do with using aspnet_compiler.exe or attempting to compile the website in updateable mode.

This line of code was causing the problem:

Dim tmpUC As ASP.usercontrols_uc_ascx = CType(LoadControl("~\usercontrols\uc.ascx"), ASP.usercontrols_uc_ascx)

It's worth noting at this point that there is a reference to the control in the web.config controls section, otherwise we'd need to use the @reference directive in the page trying to instantiate the user control programmatically.

According to this msdn article the above line should work, and when building from within visual studio or running the website directly from the code it does.

In that msdn article is also talks about including the class name in the control directive like this:

<%@ Control className="uc" %>

This is supposed to strongly type the usercontrol such that you can instantiate it directly rather than via the ASP namespace using the auto generated class name (the ones that end in _ascx). You would then rewrite the offending line like this:

Dim tmpUC As uc = CType(LoadControl("~\usercontrols\uc.ascx"), ASP.usercontrols_uc_ascx)

Except that doesn't work, you'll still get the same error: ASP.usercontrols_uc_ascx is still not defined. The reason you've just change the class name from usercontrols_uc_ascx to just uc; this class is still in the ASP namespace. So the line should now be:

Dim tmpUC As ASP.uc = CType(LoadControl("~\usercontrols\uc.ascx"), ASP.uc)

This doesn't work either (and is essentially what is shown in the msdn example). You get a similar error to before: ASP.uc is not defined.

To sum up at this point: we've essentially copied exactly what msdn did in their example, it works when you build from inside visual studio or if you run the website directly from the code causing a runtime compile, but throws error BC30002 if you try to compile in updateable mode using aspnet_compiler.exe.

So what's the solution? The answer seems to lie in ignoring msdn and striping the code back. So you still need a @reference directive at the top of the page or to include a reference to the usercontrol in web.config. This is the line that works:

Dim tmpUC As New usercontrols_UC

That's it.
As long as there is a reference to the usercontrol in either web.config or at the top of the page the class name of the usercontrol will appear at the root level in intellisense. You can specify the classname in the @control directive. You also don't need to bother with the ASP namespace (in fact it maybe this was was causing the problem).

If you use the classname attribute in the @control directive then that's what changes the name of the class in the ASP namespace from being the auto generated one. Notice how you don't need to ctype or loadcontrol; you can use the New keyword. In the example above the true class name includes 'usercontrol_' because the usercontrol in kept in a folder called user controls; if it was kept at the root level then the classname would just be 'UC'. Therefore, when looking through intellisense for the class name of the user control you have referenced remember it will probably be listed by the folder in which it's saved.

Post a comment