Skip to main content

How to add Controls Dynamically in ASP.NET

There are times when it is more practical to create a control at run time than at design time. For example, imagine a search results page in which you want to display results in a table. Because you do not know how many items will be returned, you want to dynamically generate one table row for each returned item.

Note Existing controls can often provide the functionality you get from creating controls dynamically. For example, controls such as the Repeater, DataList and RadioButtonList controls can dynamically create rows or other control elements when the page runs.

To programmatically add a control to a page, there must be a container for the new control. For example, if you are creating table rows, the container is the table. If there is no obvious control to act as container, you can use a Placeholder or Panel web server control.

In some instances, you might want to create both static text and controls. To create static text, you can use either a Literal or a Label web server control. You can then add these controls to the container as you would any other control.

Note : Manipulating controls programmatically is often used when creating composite controls.

To Add a Control to a Web Forms Page Programmatically 

Create an instance of the control and its properties

Label firstname = new Label();
firstname.Text = " Enter First Name";

Controls are typically added to the page during the page's load stage.

Add the New Control to the Controls Collection of a Container Already on the Page


Note : Because Controls is a collection, you can use the AddAt  method to place the new control at a specific location- for example, in front of other controls. However, this approach can introduce errors into the page.

The following shows the event handler for the SelectedIndexChanged event of a control called DropDownList1. The handler creates as many label controls as the user has selected from the dropdown list. The labels display the word label plus a counter. The container for the controls is a Placeholder Web server control called Placeholder1.

Note : User input in a Web Forms page can include potentially malicious client script. By default, the web forms page validates that user input does not include script or HTML elements.

Complete Code

           <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <style type="text/css">
            font-size: xx-large;
    <form id="form1" runat="server">
        <span class="style1">How many label , you want to create</span><br />
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            Height="25px" onselectedindexchanged="DropDownList1_SelectedIndexChanged" 
    <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

//Code Behind Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        int numbers = Convert.ToInt32(DropDownList1.SelectedItem.Text);
        for (int i = 0; i <=numbers ; i++)
            Label l1 = new Label();
            l1.Text = "Label" + i.ToString();
            l1.ID = "Label" + i.ToString();
            PlaceHolder1.Controls.Add(new LiteralControl("<br/>"));


Code generate the following output

How to add Controls Dynamically in ASP.NET


Popular posts from this blog

difference between structure and union in C Language

In c language article we will see the difference between union and structure. Both are the user define datatype in c language. See the table which is mentioned below: ASP.NET Video Tutorial Series Structure Union1.The keywordstruct is used to define a structure 1. The keyword union is used to define a union. 2. When a variable is associated with a structure, the compiler allocates the memory for each member. The size of structure is greater than or equal to the sum ofsizes of its members. The smaller members may end with unused slack bytes. 2. When a variable is associated with a union, thecompiler allocates thememory by considering the size of the largest memory. So, size of union is equal to the size of largest member. 3. Each member within a structure is assigned unique storage area of location. 3. Memory allocated is shared by individual members of union. 4. The address of each member will be in ascending order This indicates that memory for each member will start at different offset v…

Difference between Linear search and Binary Search in c language

SQL Video Channel : Download all SQL Video

Binary Search Linear Search Works only on sorted items. such as  1,2,3,4,5,6  etc
Works on sorted as well as unsorted items. 12,4,5,3,2,1 etc Very efficient if the items are sorted Very efficient if the items are less and present in the beginning of the list. such as Suppose your list items are : 12,3,4,5,1 and you want to search 12 number then you get beginning in the list. Works well with arrays and not on linked lists. Works with arrays and linked lists.
Number of comparisons are less More number of comparisons are required if the items are present in the later part of the array or its elements are more.

Memory representation of Linked List Data Structures in C Language

Memory representation of Linked List

             In memory the linked list is stored in scattered cells (locations).The memory for each node is allocated dynamically means as and when required. So the Linked List can increase as per the user wish and the size is not fixed, it can vary.

               Suppose first node of linked list is allocated with an address 1008. Its graphical representation looks like the figure shown below:

      Suppose next node is allocated at an address 506, so the list becomes,

  Suppose next node is allocated with an address with an address 10,s the list become,

The other way to represent the linked list is as shown below:

 In the above representation the data stored in the linked list is “INDIA”, the information part of each node contains one character. The external pointer root points to first node’s address 1005. The link part of the node containing information I contains 1007, the address of next node. The last node …