0

I'm trying to build a CompositeControl that's flexible. Depending on some of it's parameters, I would like my CompositeControl to load different user controls in it's CreateChildControls method. The exact UserControls aren't know at design time.

Just as a quick example, I tried with a "hard coded" UserControl and it failed:

protected override void CreateChildControls()
    {            
        Control UserControlControl = Page.LoadControl(typeof(MyUserControl), null);
        Controls.Add(UserControlControl);
        Label RegularControl = new Label();
        RegularControl.Text = "This gets displayed";
        Controls.Add(RegularControl);
    }

Is it possible to attain what I'm looking for?

Thanks

srmark
  • 7,942
  • 13
  • 63
  • 74

1 Answers1

2

Try the following:

protected override void CreateChildControls()
{            
            Control UserControlControl = Page.LoadControl("~/path/to/control.ascx");
            Controls.Add(UserControlControl);
}
Dan
  • 17,375
  • 3
  • 36
  • 39
  • Yup, that did it. This will do but I'd still be curious in learning how to use the other overload method with the type as argument. – srmark Jun 26 '09 at 15:16