title: Using User Controls from Other Projects in ASP
body: <p>I'm trying to re-use my ASP User Controls in other projects, and have hit upon a problem. I've read ScottGu's post on <a href="http://weblogs.asp.net/scottgu/archive/2005/08/28/423888.aspx" rel="noreferrer">http://weblogs.asp.net/scottgu/archive/2005/08/28/423888.aspx</a> and tried to follow his patterns but run-time the child controls of my controls are not being initialised.</p>
  
  <p>My setup: 
  I'm using Visual Studio 2008 
  I've created a solution to hold the controls I want to distribute to several other solutions. This solution contains:</p>
  
  <ul>
  <li>library projects (for model code, etc.), let's call this <em>"ClassLibrary"</em></li>
  <li>a WebApp project (with my User Controls), let's call this <em>"Innermost"</em></li>
  <li>a number of other WebApp projects acting as Shells for the Innermost project, let's regard one of them and call it <em>"Outer"</em></li>
  </ul>
  
  <p>The thought behind this is that Innermost contains the functionality and Outer contains any special modifications needed to make Innermost -controls work in a particular framework.</p>
  
  <p>The ClassLibrary has code like this (this is all from a demo-setup I made to make sure it was an ASP issue, not something else I was doing):</p>
  
  <pre><code>using System;
  
  namespace ClassLibrary {
      public class LibraryClass {
          public long getLong() {
              return DateTime.Now.Ticks;
          }
  
          public string getString() {
              return DateTime.Now.ToString();
          }
      }
  }
  </code></pre>
  
  <p>Innermost references the library project and works fine. This is an example of a UserControl in this project:</p>
  
  <pre><code>&lt;%@ Control Language="C#" AutoEventWireup="true" 
      CodeBehind="WebUserControl1.ascx.cs" 
      Inherits="Innermost.UserControls.WebUserControl1" %&gt;
  &lt;asp:Label ID="testLabel" runat="server"/&gt;
  </code></pre>
  
  <p>with code-behind: </p>
  
  <pre><code>using System;
  using System.Web.UI;
  using ClassLibrary;
  
  namespace Innermost.UserControls {
      public partial class WebUserControl1 : UserControl {
          protected void Page_Load(object sender, EventArgs e) {
              LibraryClass c = new LibraryClass();
              testLabel.Text = "Your number is " + c.getLong();
          }
      }
  }
  </code></pre>
  
  <p>I have a test page in the Innermost project which uses the User Controls of the Innermost project by registering them with the following syntax:</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test1.aspx.cs" 
      Inherits="Innermost.Test1" %&gt;
  
  &lt;%@ Register TagPrefix="inner" TagName="Control1" 
      Src="~/UserControls/WebUserControl1.ascx" %&gt;
  &lt;%@ Register TagPrefix="inner" TagName="Control2"
      Src="~/UserControls/WebUserControl2.ascx" %&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form id="form1" runat="server"&gt;
  &lt;div&gt;
      Control 1:
      &lt;inner:Control1 ID="control1" runat="server" /&gt;
      &lt;br /&gt;
      Control 2:
      &lt;inner:Control2 ID="control2" runat="server" /&gt;
  &lt;/div&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The Outer project references Innermost and ClassLibrary; and contains UserControls with the following registering of the Innermost controls:</p>
  
  <pre><code>&lt;%@ Control Language="C#" AutoEventWireup="true" 
      CodeBehind="TopControl.ascx.cs" Inherits="Outer.Control.TopControl" %&gt;
  &lt;%@ Register TagPrefix="inner" Assembly="Innermost"
      Namespace="Innermost.UserControls" %&gt;
  &lt;inner:WebUserControl1 ID="Control1" runat="server"/&gt;
  &lt;inner:WebUserControl2 ID="Control2" runat="server"/&gt;
  </code></pre>
  
  <p>With a simple code-behind:
      using System;
      using System.Web.UI;</p>
  
  <pre><code>namespace Outer.Control {
      public partial class TopControl : UserControl {
          public bool ShowType1 { get; set; }
  
          protected void Page_Load(object sender, EventArgs e) {
              Control1.Visible = ShowType1;
              Control2.Visible = !ShowType1;
          }
      }
  }
  </code></pre>
  
  <p>When I run the test-page in the Innermost project I get no errors, and everything works. When I run the Outer project I get <code>NullReferenceException</code>s telling me that the child controls of the <code>Innermost.UserControls.WebUserControlX</code> have not been set.</p>
  
  <p>StackTrace:</p>
  
  <pre><code>[NullReferenceException: Object reference not set to an instance of an object.]
     Innermost.UserControls.WebUserControl1.Page_Load(Object sender, EventArgs e) in c:\Test\ASP\Innermost\Innermost\UserControls\WebUserControl1.ascx.cs:9
     System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
     System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
     System.Web.UI.Control.OnLoad(EventArgs e) +99
     System.Web.UI.Control.LoadRecursive() +50
     System.Web.UI.Control.LoadRecursive() +141
     System.Web.UI.Control.LoadRecursive() +141
     System.Web.UI.Control.LoadRecursive() +141
     System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
  </code></pre>
  
  <p>I can provide the test setup as solutions if anyone wants to have a closer look at this.</p>
  
  <p>So, finally, my question is: Why do the ChildControls of the controls not get set up as expected? Even if I run <code>CreateChildControls()</code> in the Init of the outer controls they aren't being created. Is there some particular way I need to compile the Innermost project for use in the Outer project?</p>
  
  <p>Any help would be much appreciated!</p>