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

Top 5 Contributors of the Month
Gaurav Pal

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Dynamically create multiple rows of controls in an asp.net web page

Posted By:Rajeev Kumar Mehta       Posted Date: June 09, 2010    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  

This articles illustrates how to create multiple rows of controls in an asp.net page using table,session and viewstate


This articles illustrates how to create multiple rows of controls in an asp.net web page dynamically using C# code with the help of a Panel,table,Session and ViewState.



Here i am using a panel as a place holder for the table.

First,I am adding a textbox to a cell and a checkbox to another cell,then i am adding these two cells to a row of the table.

Here,i am using Session variable to store the state of the table which will be used after postback to restore the state of the table.Here,we can't use viewstate for storing the state of the table. I am using a ViewState variable to hold the state of the counter which store the number of the controls already created.

I am giving unique id for the textboxes and the checkboxes with the help of this counter.

Note - If we do not store the state of the table and the counter then the values of counter will le lost after the postback and also the state of the table will be lost after the postback.

Look at the code given below,it is very simple !!


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 = 1;

    private int ctr = 0;

    Table table = null;

    protected void Page_Load(object sender, EventArgs e)

        if (!IsPostBack)

            table = new Table();

            table.ID = "tableBuild";

            Session["table"] = table;

            ViewState["ctr"] = ctr;


        ctr = (Int32)ViewState["ctr"];

        table = (Table)Session["table"];



    private void GenerateTable(int colsCount)


        TableRow row = new TableRow();

        TableCell cell = new TableCell();

        TextBox tb = new TextBox();

        tb.ID = "TextBoxRow_" + ctr;

        tb.Width = 80;

        // Add the control to the TableCell


        // Add the TableCell to the TableRow


        TableCell cell1 = new TableCell();

        CheckBox cb = new CheckBox();

        cb.ID = "chkBox" + ctr;

        cb.Width = 40;

        // Add the control to the TableCell


        // Add the TableCell to the TableRow


        // Add the TableRow to the Table


        Session["table"] = table;

        ViewState["ctr"] = ctr;


    protected void btnAdd_Click(object sender, EventArgs e)

        numOfColumns = 1;

        //Generate the Table based from the inputs





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

Missing my parents


All the queries are welcome !!

Rajeev Kumar Mehta

EMail - mikkykumar@gmail.com



 Subscribe to Articles


Further Readings:

Author: JH         Company URL: http://www.dotnetspark.com
Posted Date: June 09, 2010


As per my understanding you are adding new with controls (Textbox,checkbox) usng button

btadd_click event handler..

well you have used viewstate for holding state of row counter.

why you can use static variable for holding the row counter variable ctr. because

ViewState is expensive..i.e performanace wise..

Can you try static variable instead of using viewstate. ?


Author: Rajeev Kumar Mehta         Company URL: http://www.dotnetspark.com
Posted Date: June 10, 2010

The problem with the static variable is that if there are more than one user are login simultanously then if for one user the value of counter is incremented then other user will get this incremented value of the counter.

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

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