Can I dynamically create custom controls using constructors?

I have a user control that will dynamically add other user controls to it depending on user settings. I'm having problems adding controls using the overloaded LoadControl method.

I tried to do this:

ddControl = typeof(DropDownControl), new object[] { testData, FilterModel.Models[i] });

However, none of the controls in ddControl was initialized, and when I tried to set the data source to its contents, it exploded. I pass the model and data source to the constructor, which sets the properties, default values, etc. Should I create a LoadModel () method to load model values ​​after creating the control, for example

(DropDownControl)LoadControl("~/WebUserControls/DropDownControl.ascx")

or can I do this through the constructor somehow, declaring it dynamically?

If this is unclear, let me know how I can do this. Thank!

+5
source share
1 answer

You can use PlaceHolderto dynamically add these controls and perform other operations, exposing a method for this, and in this way you can control everything without interfering with the life cycle of your control.

Instead of using constructors, do not use properties and methods.

0
source

All Articles