.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

How to add controls Dynamically in a asp.net page

Posted By:Rajeev Kumar Mehta       Posted Date: May 07, 2010    Points: 10    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

Sample code to illustrates How to add controls dynamically in an asp.net web page using C#
 

Topic : How To Create Controls in asp.net programmatically

 I am creating a row of controls dynamically which includes one dropdownlist, 12 textboxex and one checkbox as  soon as user click on a Add New Row button.

Textboxes are kept for receiving inputs for the values of 12 months.

Here I am taking a panel and then I am adding the newly created table to this panel which serves as a placeholder for the table.

Inside the table I am adding these controls. Look the code it is very easy

Code Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    private int numOfColumns = 12;
    public static int ctr = 0;
   static Table table = new Table();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        table.ID = "table1";
        Panel1.Controls.Add(table);
    }
    protected void btnAddNewRow_Click(object sender, EventArgs e)
    {
        numOfColumns = 12;

        //Generate the Table based from the inputs

        GenerateTable(numOfColumns);
    }
    private void GenerateTable(int colsCount)
    {

        ctr++;
        // Now iterate through the table and add your controls 
        TableRow row = new TableRow();

        row.ID = "row" + ctr;

        TableCell cell1 = new TableCell();

        DropDownList dl = new DropDownList();
        dl.ID = "DrpDwnBrand" + ctr;
        dl.AutoPostBack = false;


        dl.Width = 135;
        // Add the control to the TableCell

        cell1.Controls.Add(dl);
        // Add the TableCell to the TableRow

        row.Cells.Add(cell1);

        for (int j = 1; j <= colsCount; j++)
        {
            TableCell cell2 = new TableCell();
            TextBox tb = new TextBox();

            if (j == 1)
            {
                tb.ID = "txtJan" + ctr;
            }
            else if (j == 2)
            {
                tb.ID = "txtFeb" + ctr;
            }
            else if (j == 3)
            {
                tb.ID = "txtMar" + ctr;
            }
            else if (j == 4)
            {
                tb.ID = "txtApr" + ctr;
            }
            else if (j == 5)
            {
                tb.ID = "txtMay" + ctr;
            }
            else if (j == 6)
            {
                tb.ID = "txtJun" + ctr;
            }
            else if (j == 7)
            {
                tb.ID = "txtJul" + ctr;
            }
            else if (j == 8)
            {
                tb.ID = "txtAug" + ctr;
            }
            else if (j == 9)
            {
                tb.ID = "txtSep" + ctr;
            }
            else if (j == 10)
            {
                tb.ID = "txtOct" + ctr;
            }
            else if (j == 11)
            {
                tb.ID = "txtNov" + ctr;
            }
            else if (j == 12)
            {
                tb.ID = "txtDec" + ctr;
            }


            tb.Width = 37;

            // Add the control to the TableCell

            cell2.Controls.Add(tb);

            // Add the TableCell to the TableRow

            row.Cells.Add(cell2);

        }
        TableCell cell3 = new TableCell();

        CheckBox cb = new CheckBox();
        cb.Controls.Clear();
        cb.ID = "chkSameAll" + ctr;
        cb.AutoPostBack = false;
        cb.Width = 10;

        // Add the control to the TableCell

        cell3.Controls.Add(cb);

        // Add the TableCell to the TableRow

        row.Cells.Add(cell3);

        // Add the TableRow to the Table
        table.Rows.Add(row);

    }  
}

Default.aspx

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





    Untitled Page


    
    





     

Further Readings:

Responses
Author: aaditya         Company URL: http://www.dotnetspark.com
Posted Date: January 21, 2011

Hey awesome post, just what is was looking for.
I need another favor. I am looking to populate the text boxes based on the drop down selection. The drop downs are populate form the database and I figured a way for it.
Can you help me with the code for populating the text boxes onselectionindex of the drop down box.

thanks
Aaditya

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend