title: Nested User Controls not working within Visual Web Part
body: <p>Despite my best efforts, I've been tasked by my employer to develop a web application using Sharepoint, instead of something useful.</p>
  
  <p>I'm having trouble embedding User Controls into Visual Web Parts in a Sharepoint project in Visual Studio 2010.  I'm using the following steps (note that this code is just a synopsis of what I have- as my real code currently contains enough curse words to get me banned from StackOverflow for life):</p>
  
  <ol>
  <li><p>Create a new, empty Sharepoint project.</p></li>
  <li><p>Add a User Control to the project (located in ControlTemplates), named MyControl.ascx</p></li>
  </ol>
  
  <p>MyControl.ascx:</p>
  
  <pre><code>&lt;asp:TextBox runat="server" ID="txtStuff" /&gt;
  </code></pre>
  
  <p>MyControl.ascx.cs:</p>
  
  <pre><code>public class MyControl : UserControl 
  {
      public int Property { get; set; }
  
      protected void Page_Load(object sender, EventArgs e) 
      {
          txtStuff.Text = "I hate you sharepoint";
      }
  }
  </code></pre>
  
  <ol>
  <li>Add a Visual Web Part to the project, MyWebPart.ascx</li>
  </ol>
  
  <p>MyWebPart.ascx</p>
  
  <pre><code>&lt;%@ Register TagPrefix="c" TagName="MyControl" src="~/_controlTemplates/MyControl" %&gt;
  &lt;c:MyControl runat="server" ID="mycontrol" /&gt;
  </code></pre>
  
  <p>When I get to this point, I don't get any intellisense for MyControl, more than likely because VS doesn't know of the existence of the .ascx file.  I also get the green squiggly line under the control name letting me know it doesnt exist.  However, I can still deploy the project to my Sharepoint server itself and all will work well.</p>
  
  <p>However, this falls apart when I need to do anything useful interacting with the control itself:</p>
  
  <p>MyWebPart.ascx.cs</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e) {
      mycontrol.Property = 1234;
  }
  </code></pre>
  
  <p>Which yields an error of "System.Web.UI.UserControl does not contain a definition of 'Property' and no extension method...'.  The designer.cs file indeed specifies the control as a UserControl, and not its real type.</p>
  
  <p>Since this now yields a build error, I can't deploy the project to the Sharepoint server, so I can't really work around this intellisense issue.</p>
  
  <p>I've found this to be very inconsistent behavior; sometimes the intellisense will appear, depending on whether or not the project is currently deployed to the server.  However, there's not a predictable set of steps I've been able to take to get the development environment in a working state.</p>
  
  <p>Any thoughts?  Things I've tried:</p>
  
  <ol>
  <li><p>Right clicking on the web part's ASCX and clicking "Open With..." and choosing the "With Encoding" option.  No effect.</p></li>
  <li><p>Developing with the application both deployed and retracted, and various combinations of closing and reopening the .ascx files, the solution, and Visual Studio itself.  Sometimes works, sometimes doesn't.</p></li>
  <li><p>Disabled the project's "Auto-Retract after Debugging" option (recommended from a knowledge base article).  No effect.</p></li>
  </ol>