How to load user controls dynamically?You might face situations where you have to add the user controls to your page dynamically. Based on some criteria you will decide during runtime whether to load a user control or not. For this purpose there are methods like LoadControl and Controls.Add. The LoadControl method is used to load a control to the page and this is done in the Page_Load event of the page in which it is loaded. The name of the user control is given an argument to the LoadControl method to load that user control. The Controls.Add method is used to place the user control on the page. But merely using this method will place it on your page but not sure where it will place. So you are in need of a placeholder to add this control. For example consider the code given below, PlaceHolder_Ctrl.Controls.Add(name_of_UserControl); With the above syntax you can add the user control to any placeholder. You can also use panel control which can hold the user control. Thus you can add a user control dynamically to the web page.
|