-->

How to add usercontrol dynamically in ASP.NET.

Introduction

In this post, I am explain how to add usercontrol dynamically in ASP.NET.

Steps :

Step - 1: Create New Project.

Go to File > New > Project > Select asp.net web forms application > Entry Application Name > Click OK.

Step-2: Add a Usercontrol.

Go to Solution Explorer > Right Click on Solution Explorer > Add > New item > Select Web User Control Under Web > Enter name > Add.

Step-3: Add a Webpage and Design for Add Usercontrol dynamically.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select web form/ web form using master page under Web > Enter page name > Add.

HTML Code
    <h3>Add Usercontrol Dinamically in ASP.NET</h3>
   <div>
       <asp:Button ID="btnAddWeatherUC" runat="server" Text="Add Weather Gadgets" OnClick="btnAddWeatherUC_Click" />
   </div>
    <div>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    </div>
        

Step-4: Write code in button_Click event for add Usercontrol into page dynamically.


   
            protected void btnAddWeatherUC_Click(object sender, EventArgs e)
            {
                Control ctl = Page.LoadControl("~/UCWeatherReport.ascx");
                PlaceHolder1.Controls.Add(ctl);
            }
        

Step-5: Run Application.

Hello ! My name is Sourav Mondal. I am a software developer working in Microsoft .NET technologies since 2010.

I like to share my working experience, research and knowledge through my site.

I love developing applications in Microsoft Technologies including Asp.Net webforms, mvc, winforms, c#.net, sql server, entity framework, Ajax, Jquery, web api, web service and more.