Adding Web Forms Controls Dynamically in ASP.Net C# Web Application


As requirements grpw in today's applications there is a need to add controls dynamically as per requirements in applications at runtime, so by considering the above requirements I have decided to write this article by considering both beginners and whoever wants to know how to add controlls dynamically at runtime. So let us start with the basics.
What is Controls ?
ASP.NET Web server controls are objects in ASP.NET Web pages that run when the page is requested and that render markup to a browser. These controls are used in forms to take input data or to show output data to the user. These controls are a very important part of any application.
All the web controls are derived  from a common base class System.Web.UI.Control.
Textbox, DropDownlist, Gridview, Label, Button and so on
What Means dynamic Conrols ?
The controls that are added automatically to forms at runtime according to requirements which are not defined in the web page at design time of the form are called dynamic conrols.
You need to add a condition to create controls on a button click event or another event.
Why Need of dynamic controls ?
These types of controls are needed, when there is a need of many controls in one form but you do not want to make the web page ugly from an abundance of controls. To do that time we can simply create the controls according to the form's requirements which change according to the user in a single form.
Now let us start for creating a one sample web application to implement this requirement.
Now let us start to create a Website as:
  1. Start - All Programs - Microsoft Visual Studio 2010.
  2. File - New Website - C# - Empty website (to avoid adding a master page).
  3. Give the web site a name such as AddingDynamic_controls or another as you wish  and specify the location.
  4. Then right-click on Solution Explorer - Add New Item - Default.aspx page.
  5. Drag and drop one button on the <form> section of the Default aspx page.
Then switch to design view and double-click on "Button" then in the <form> section of the Default aspx page source will look like as in the following:
                    <asp:ButtonID="Button1"runat="server"Text="Add"onclick="Button1_Click" />
Now switch to Default.aspx.cs and use the following code in the button click event:
 protected void Button1_Click(object sender, EventArgs e)
        for (int i = 0; i < 4; i++)      //specifying the condition in for loop for creating no. of textboxes
      TextBox tb=new TextBox ();   //Create the object of TexBox Class
      tb.ID = i.ToString();                // assign the loop value to textbox object for dynamically adding Textbox ID
      Form.Controls.Add(tb);        // adding the controls

Now run the application and click on the add button. It will add four Textboxes into the running web page dynamically as shown in the following image:

In the above example you see that the four textboxes are added to the form which were not defined at the time of form design. You can create any type of control dynamically; suppose
if you want to create a Dropdwonlist dynamically then use the following code in the button click:
  DropDownList db=new  DropDownList  ();   //Create DropDownList
Similar to this, you can create any web control.
I hope this article is useful for all readers and for more code please download the zip file of the sample application. If you have any suggestion then please contact me.