Unfortunatley ASP.NET does not seem to support any easy factory creation pattern for controls. However, in 3.5 you are given very fine grained control over the actual code that the ASP.NET runtime generates for your .aspx file.
Simply apply [ControlBuilder(...)] attributes to all the controls that you want constructed by your container. Subclass ControlBuilder and override ProcessGeneratedCode to replace the constructor with a call to your container.
Here's a simple example:
public class ServiceProviderBuilder : ControlBuilder
{
public override void ProcessGeneratedCode(System.CodeDom.CodeCompileUnit codeCompileUnit, System.CodeDom.CodeTypeDeclaration baseType, System.CodeDom.CodeTypeDeclaration derivedType, System.CodeDom.CodeMemberMethod buildMethod, System.CodeDom.CodeMemberMethod dataBindingMethod)
{
// search for the constructor
foreach (CodeStatement s in buildMethod.Statements)
{
var assign = s as CodeAssignStatement;
if (null != assign)
{
var constructor = assign.Right as CodeObjectCreateExpression;
if (null != constructor)
{
// replace with custom object creation logic
assign.Right = new CodeSnippetExpression("("+ ControlType.FullName + ")MyContainer.Resolve<" + ControlType.BaseType.FullName + ">()");
break;
}
}
}
base.ProcessGeneratedCode(codeCompileUnit, baseType, derivedType, buildMethod, dataBindingMethod);
}
}
[ControlBuilder(typeof(ServiceProviderBuilder))]
public partial class WebUserControl1 : System.Web.UI.UserControl
{
public WebUserControl1()
{
}
protected void Page_Load(object sender, EventArgs e)
{
}
}