ASP.NET Core 1.1 Hosting: How To Make Dynamic Control In ASP.NET Using Panels?

In this tutorial, we will learn about dynamic control and how to add it in ASP.NET application using C#. The objective is simple; we will just add some basic controls in runtime. We will use some panels to adjust our dynamic control based on the control container.
Well, this tutorial is not as worthy as it should be because generally the objectives that we include here are quite simple and also in real development, the dynamic control that we are running is never placed like that. The so called dynamic is actually used when we need the resources, and we don’t want to use the whole IDE, at that time such dynamic controls are quite useful.

Open your Visual Studio 2010 and create an Empty Website. Give a suitable name [dynamic_demo].

Step 2
In Solution Explorer, you get your empty website. Add a web form –

For Web Form


Step 3
Open your dynamic_demo.aspx, and drag and drop the table. Inside one of the td  tags, you have to place checkboxlist, for prompting the user to what controls they want to generate. So, we had taken almost 5 controls as – Textbox, button, label, dropdown, and radiobutton. After that, we will place one textbox to prompt the user to choose how many controls they want to add. Then, a simple button control is placed to generate the control dynamically. Your design will look like the below figure.
Here, after every control, we have placed a panel, so that the dynamic control is placed at this appropriate place. We hope that for the above design, we do not have to share the whole td and tr tag code because it is a lengthy table. For simplicity, we have shared our code too, you can refer that in case any trouble occurs.

Step 4
Open dynamic_demo.aspx.cs file to write our code. Here, we will  first check what controls the user wants to generate. After getting that, we will count how many controls he wants. Suppose, he puts 3, then every control will make 3 dynamic controls.


The last two methods are specifically for the textboxes. You will see that when you generate the textbox dynamically, there will be a default text written inside the Textbox, something like Textbox1, Textbox 2 and so on based on the control you had generated. To remove that, you have to click inside the textbox and then use backspace key.

The approach is tedious, so we have used these two methods which will automatically remove the default text when you focus on the control.