Vinz' Blog

"Code, Beer and Music" ~ my way of being a programmer!
posts - 124, comments - 367, trackbacks - 0

My Links

News

Archives

Image Galleries

Dynamically Adding TextBox Control to ASPNET Table

This demo shows on how to generate Table with TextBoxes dynamically based from the number of Columns and Rows entered from the TextBox control and print the values of the dynamically added TextBox on the page. See the screen shot below:

 


 

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 then let’s grab the values entered from the dynamic TextBox using Request.Form method. Here’s the code block below.

 

protected void Button2_Click(object sender, EventArgs e)

{

    //Check if ViewState values are null

    if (ViewState["cols"] != null && ViewState["rows"] != null)

    {

        //Find the Table in the page

        Table table = (Table)Page.FindControl("Table1");

        if (table != null)

        {

            //Re create the Table with the current rows and columns

            GenerateTable(int.Parse(ViewState["cols"].ToString()), int.Parse(ViewState["rows"].ToString()));

           

            // Now we can loop through the rows and columns of the Table and get the values from the TextBoxes

            for (int i = 0; i < int.Parse(ViewState["rows"].ToString()) ; i++)

            {

                for (int j = 0; j < int.Parse(ViewState["cols"].ToString()); j++)

                {

                    //Print the values entered

                    if (Request.Form["TextBoxRow_" + i + "Col_" + j] != string.Empty)

                    {

                        Response.Write(Request.Form["TextBoxRow_" + i + "Col_" + j] + "<BR/>");

                    }

                }

            }

        }

    }

}

 

 

That’s it! Hope this example is useful for you!

 

Print | posted on Tuesday, March 17, 2009 8:17 PM |

Feedback

Gravatar

# re: Dynamically Adding TextBox Control to ASPNET Table

super!! cheers to ur beer
3/24/2009 10:30 PM | bavin
Gravatar

# re: Dynamically Adding TextBox Control to ASPNET Table

super!! cheers to ur beer
3/24/2009 10:30 PM | bavin
Gravatar

# re: Dynamically Adding TextBox Control to ASPNET Table

Nice Code.
I need more help.
How can i insert value from the textbox generated dynamically under a button.
Please help me.
4/26/2009 6:04 PM | Biswajit
Gravatar

# re: Dynamically Adding TextBox Control to ASPNET Table

Hi Biswajit,
You mean insert it to the database?
4/26/2009 9:59 PM | Vinz
Gravatar

# re: Dynamically Adding TextBox Control to ASPNET Table

Hi,

I appreciate the coding provided. It helped me a lot to get through.

But i have a doubt, Can i read text boxes without creation of Table object e.g if i have to create 10 text boxes on each button click event and after creating 10 text boxes, i have to read the values from all the textboxes. How can i do that?

Any suggestion would be highly appreciable.

Thanks in advance.

Good work.

Thanks,
-Vivek Jain
6/7/2009 1:57 PM | Vivek Jain
Gravatar

# re: Dynamically Adding TextBox Control to ASPNET Table

@ Vivek Jane,

You could use the Request.Form method for getting the data from the TextBox.
6/7/2009 8:39 PM | Vinz
Gravatar

# re: Dynamically Adding TextBox Control to ASPNET Table

Nice post.. Thanks a lot to author... and cheers to viewers!!!!!
6/12/2009 6:50 AM | vijayanand
Gravatar

# re: Dynamically Adding TextBox Control to ASPNET Table

it really helps me a lot.
Thanks!
6/16/2009 1:40 PM | vista wallpaper
Gravatar

# re: Dynamically Adding TextBox Control to ASPNET Table

I am dynamicallly adding fileupload control in the table but in every postback to the page file upload control lost its filename and i cant get that filename.I had three dropdown list control in the page and each one have autopostback property true.So how can i get the filename of that fileupload control.Please help me.
6/17/2009 11:02 PM | PRATIKSHA
Gravatar

# re: Dynamically Adding TextBox Control to ASPNET Table

Hi PRATIKSHA,

Since you are using the ASPNET FileUpload control then that is an expected behavior..What i mean is that the FileUpload control will CLEAR the file path displayed in the TextBox when the page is posted back to the server. and i think it was design for security purposes. See link below for more informations

http://forums.asp.net/t/1201039.aspx
6/17/2009 11:12 PM | Vinz
Gravatar

# re: Dynamically Adding TextBox Control to ASPNET Table

Dynamically Adding TextBox Control to ASPNET Table
very-very Thanks a lot to author...
6/25/2009 1:58 AM | sanjeev Kumar Singh
Gravatar

# re: Dynamically Adding TextBox Control to ASPNET Table

Thanks a lot.. nice code..
But what if i want these text boxes at a particular position.. can i fix starting postion ...
6/30/2009 12:18 AM | Rana
Gravatar

# re: Dynamically Adding TextBox Control to ASPNET Table

Hi Rana,

You could probably add a TextBox in a particular position by writing a condition for checking the rows and columns count.

Here's a quick example:

for (int i = 0; i < rowsCount; i++)
{
TableRow row = new TableRow();
for (int j = 0; j < colsCount; j++)
{
TableCell cell = new TableCell();

if (j == 0) // Adds a TextBox to the first column of the Table
{
TextBox tb = new TextBox();
tb.ID = "TextBoxRow_" + i + "Col_" + j;
cell.Controls.Add(tb);
row.Cells.Add(cell);
}
}
table.Rows.Add(row);

}
6/30/2009 11:49 PM | Vinz
Gravatar

# re: Dynamically Adding TextBox Control to ASPNET Table

how to add two dynamically created text
8/2/2009 4:56 PM | praveen
Gravatar

# re: Dynamically Adding TextBox Control to ASPNET Table

@praveen,

can you please elaborate more? Where do you want to add those dynamic text?
8/2/2009 5:00 PM | Vinz
Gravatar

# re: Dynamically Adding TextBox Control to ASPNET Table

Great Code
Thanks
8/4/2009 12:19 AM | Satish
Gravatar

# re: Dynamically Adding TextBox Control to ASPNET Table

excellent,its working,thanks
10/13/2009 11:02 PM | Narayanan
Gravatar

# re: Dynamically Adding TextBox Control to ASPNET Table

how to add textbox to a table already in the page
10/29/2009 2:34 AM | jjelv
Gravatar

# re: Dynamically Adding TextBox Control to ASPNET Table

@jjelv,
Maybe you are looking for something like this:
http://geekswithblogs.net/dotNETvinz/archive/2009/06/29/faq-dynamically-adding-rows-in-asp-table-on-button-click.aspx
10/29/2009 2:22 PM | Vinz
Gravatar

# re: Dynamically Adding TextBox Control to ASPNET Table

exactly !!
thank you for this excellent code
10/30/2009 12:55 AM | jjelv
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: