Dynamic Creation of User Controls
Instantiating a new user control is not as straightforward as you'd think. The logical approach would be like:
UserControl control = new UserControl();
Unfortunately, this does not work. While you've technically got an instantiated UserControl object, none of the controls that should be contained by the user control will be there - they will all be null. Why, not quite sure. But at least there's an alternate approach.
You have to use the LoadControl method:
UserControl control = LoadControl(”~/MyControls/UserControl.ascx”) as UserControl;
This way you get a fully fleshed-out UserControl with all the child controls.
This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available in our efforts to advance understanding of environmental, political, human rights, economic, democracy, scientific, and social justice issues, etc. We believe this constitutes a 'fair use' of any such copyrighted material as provided for in section 107 of the US Copyright Law. In accordance with Title 17 U.S.C. Section 107, the material on this site is distributed without profit to those who have expressed a prior interest in receiving the included information for research and educational purposes. For more information go to: http://www.law.cornell.edu/uscode/17/107.shtml
. If you wish to use copyrighted material from this site for purposes of your own that go beyond 'fair use', you must obtain permission from the copyright owner.