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.





______________________________________________________

Recommended Resource



| Converting Web Pages to User Controls in .NET | .Working on .NET User Controls | Overview of ASP.NET User Controls | Web User Controls - Creation and Implementation | Advantages of using User Controls in ASP.NET | Re-usability of User Controls in ASP.NET | Personalizing Web Application by Creating User Controls in .NET | Customizing your Application with VB.net User Controls | Building User Controls in ASP.NET | Examples of User Controls in VB.NET | Example of C# User Controls | Where to use Visual Basic User Controls | Working on User Interface Controls | Creating Dynamic User Controls | Understanding VB .NET User Controls | Creating User Controls VB.NET | How to load user controls dynamically |

 

“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.