Can’t access public properties of registered User Controls inside a Sharepoint 2010 Visual Web Part


I was trying to use a user control within a Sharepoint 2010 Visual Web Part and can't seem to access any of the public properties of the user controls.


Casting the user control explicitly seem to do the trick. But I don't want to do that every time!


What's really happening

When I created the dependant user control that the visual web part require, VS2010 automatically create a special Sharepoint's Control Template folder for me : which is kinda neat.


Worth pointing out that the User control has a ’˜deployment location' property where it says to which folder it's going to be deployed to.

In the Visual Webpart, I've registered the user control as such.

<%@ Register Tagprefix="UserControl" TagName="NotificationList" Src="~/_ControlTemplates/WebParts/NotificationList.ascx" %>

Note that the source is _ControlTemplates rather than ControlTemplates. If I had omitted the underscore it wouldn't work as there isn't such folder in Sharepoint. I worked this out by looking at the path which the generated Visual Web Part bootstrapper point to .

If you look closer, Visual Studio 2010 doesn't seem to like our user control registration.


It's suggesting that it can't find the control and decided to put the base class System.Web.UI.UserControl in our Visual Web Part designer class (* This is the culprit of why we can't get the public properties/methods without explicit cast.

Seems the problem is caused by Visual Studio 2010 not having the same assumptions with the Sharepoint 2010 runtime. It's really a VS2010 Bug.

VS2010 should've generated \_ControlTemplates\ in the first place when I create user controls. Alternatively, VS2010 should simply accept that ControlTemplates is a merely string token that will be converted to _ControlTemplates by Sharepoint 2010 runtime and take this account when generating the designer classes.


The solution is very simple:

  1. Register the user control as above : i.e point src to ~/_ControlTemplates/ ’¦
  2. Simply rename the ControlTemplate solution folder in VS2010 to _ControlTemplate


As soon as I did this, VS2010 T4 template (which generates the designer classes) behaves properly and casts my user controls to the proper types.

  1. Markus J says:

    I just figured this out... right before I stumbled upon your post! Arghhh... Over a year later and there isn't a patch/fix for this in VS2010? Thanks for sharing though... keep up the good work dude.