To add a usercontrol in codebehind there are two ways.
- Exposing a usercontrol constructor to the parent page.
- Using Page.LoadControl method.
So basically there are two different ways of doing the same task. Do one method work under certain circumstances and the other does not?What’s the difference in how they work? and when to prefer one over the other?
LoadControlis primarily used to dynamically add a User Control to a page when the type is unavailable. Most user controls are unavailable in updatable website applications. Also note that because the type is unavailable, properties of the dynamically created user control are difficult to set.MyControl c = new MyUserControl()is preferred, but doesn’t stop you from doing something like:However, I don’t see the need.