You need to Load the control from code instead of the markup. That is due to how the asp.net compiler creates the assemblies from your mark up.
I made the following changes to load the control from the codebehind. I had to change the databinding as well by changing the # to = (and have properties on the control).
Control MenuButton.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MenuButton.ascx.cs" Inherits="WebApplication1.MenuButton" %>
<li id="Li1" runat="server">
<% if (Children.Count == 0)
{ %>
<a href="<%= Url %>"><%= Description %></a>
<% }
else
{
%>
<a href="<%= Url %>" class="dropdown-toggle" data-toggle="dropdown">
<%= Description %><b class="caret"></b></a>
<!-- is now a server control -->
<ul class="dropdown-menu multi-level" runat="server" ID="ul1">
<!-- the page_load adds MenuButtons here -->
</ul>
<%
}
%>
</li>
Codebehind MenuButton.ascx.cs
public partial class MenuButton : System.Web.UI.UserControl
{
public String Url { get; set; }
public string Description { get; set; }
public List<MenuItem> Children { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (Children != null)
{
foreach (var menuItem in Children)
{
// Create a button from the ascx file
var but = (MenuButton)LoadControl("/MenuItems/MenuButton.ascx");
// bind!
but.Children = menuItem.Children;
but.Description = menuItem.Description;
but.Url = menuItem.Url;
if (ul1 != null)
{
// add our button
ul1.Controls.Add(but);
}
}
}
}
}
ViewModel
public class MenuItem
{
public List<MenuItem> Children { get; set; }
public String Url { get; set; }
public string Description { get; set; }
}