Introduction
In this article, we will try to learn how we can load a user control dynamically using C#. So, let's start by adding an .aspx page and a user control on our page. We name this control as DummyControl.ascx. On the main page, we will add an ASP panel, say pnlDynamicControl. So, our page markup will look like the following.
<form id="form1" runat="server">
<div>
<asp:Panel ID="pnlDynamicControl" runat="server">
</asp:Panel>
</div>
</form>Next, add some dummy text on the user control. Now, let's move into the code-behind of the page and write the code to load the user control into the panel. We will do this by using the LoadControl method of the Page class. This method expects a parameter, which is the url of the user-control inside the project. So, our code will look like the following.
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Load the control
Control _dummyUserControl = (Control)Page.LoadControl("DummyControl.ascx");
// Add the control to the panel
pnlDynamicControl.Controls.Add(_dummyUserControl);
}
}That's it, we are done. Run the application and see the results.

Read more articles on C#


nitin patilPosted Apr 20, 2016, 1:12 AM EDT
good one..
Kuppurasu NagarajTOP 100Posted Apr 19, 2016, 9:07 AM EDT
Nice..
Debasis SahaTOP 100Posted Apr 19, 2016, 6:34 AM EDT
Good One..
Shakti Singh DulawatPosted Apr 19, 2016, 5:32 AM EDT
Cool
Kashif SohailPosted Apr 18, 2016, 10:52 PM EDT
Short but helpful
Humayun Kabir MamunPosted Apr 18, 2016, 10:52 PM EDT
Nice...
AniPosted Apr 18, 2016, 10:14 PM EDT
Thanks for sharing
Talha Bin AfzalPosted Apr 18, 2016, 9:23 PM EDT
Good
Dharmesh SinghPosted Apr 18, 2016, 8:55 PM EDT
good