-->

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
  1. <h3>Add Usercontrol Dinamically in ASP.NET</h3>
  2. <div>
  3. <asp:Button ID="btnAddWeatherUC" runat="server" Text="Add Weather Gadgets" OnClick="btnAddWeatherUC_Click" />
  4. </div>
  5. <div>
  6. <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
  7. </div>

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


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

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.

Related Posts: