User Id :    Password :      New Member   Forgot Password  
 
Dynamically Adding TextBox Control to asp.net Table
Description how to generate Table with TextBoxes dynamically based from the number of Columns and Rows entered from the TextBox control   No. of Views     7127
  Rating     4
Author Gaurav   Posted On     16 Mar 2010
Tags ASP.NET,Server Control    

Sample Code   Download Code

This demo shows on how to generate Table with TextBoxes dynamically based from the number of Columns and Rows entered from the TextBox control To start, let’s declare the following global variables below:
private int numOfRows = 0;
private int numOfColumns = 0;
Here’s the code block for the Generating the Tables with TextBoxes.
 private void GenerateTable(int colsCount, int rowsCount)
{
        //Creat the Table and Add it to the Page
        Table table = new Table();
        table.ID = "Table1";
        Page.Form.Controls.Add(table);
        // Now iterate through the table and add your controls 
        for (int i = 0; i < rowsCount; i++)
        {
            TableRow row = new TableRow();
            for (int j = 0; j < colsCount; j++)
            {
                TableCell cell = new TableCell();
                TextBox tb = new TextBox();
                // Set a unique ID for each TextBox added
                tb.ID = "TextBoxRow_" + i + "Col_" + j;    
                // Add the control to the TableCell
                cell.Controls.Add(tb);
                // Add the TableCell to the TableRow
                row.Cells.Add(cell);
            }
            // Add the TableRow to the Table
           table.Rows.Add(row);
        }
}
As you can see from above code, we just simply create a blank table and add it to the form and then fill the table with columns and rows of TextBoxes based from the values of rowsCount and colsCount Now let’s call the method above on Page_Load event to recreate the Table when it post backs.
protected void Page_Load(object sender, EventArgs e)
{
         // the two paramters are declared globally
         GenerateTable(numOfColumns, numOfRows);
}
 
Now let’s call the method above on Button Click event (Generate Table) and pass the necessary parameters needed, see below code block.
protected void Button1_Click(object sender, EventArgs e)
{
        //Check if the inputs are numbers
        if (int.TryParse(TextBox1.Text.Trim(), out numOfColumns) && int.TryParse(TextBox2.Text.Trim(), out numOfRows))
        {
            //Generate the Table based from the inputs
            GenerateTable(numOfColumns, numOfRows);

            //Store the current Rows and Columns In ViewState as a reference value when it post backs
            ViewState["cols"] = numOfColumns;
            ViewState["rows"] = numOfRows;
        }
        else
        {
            Response.Write("Values are not numeric!");
        }
}
 
Since we are done on creating our Table with TextBoxes dynamically

About Author

About Author I am as simple as c#. Gaurav
No Photo
 
Country India
Company N/A
Home Page N/A

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

 
 
Posted By Gaurav on 02 Jul 2010 at 10:34 PM
 
Comment
 
 
   
Write your comment here.
Comment
Verification Code