How to Dynamically Add a UserControl to a Page To dynamically add a usercontrol to a .NET page: You cannot simply instantiate the control and add it to the controls collection. Instead, use the LoadControl method, and then add it to a placeholder or other control where it should appear: Correct: var form = (MyControl)LoadControl("Controls/MyControl.ascx"); myPlaceholder.Controls.Add(form); INCORRECT: var form = new MyControl(); myPlaceholder.Controls.Add(form); If you do it the incorrect way, the user control will be invisible and all it's child controls will be null.
Created By: amos 12/1/2014 5:59:25 PM
|
|