简体   繁体   中英

Add dynamically text from textbox to table

I have 3 TextBoxes for user input. I want the user to enter name, email and age and then clicking on a button to put it into a table.

I have created it using design mode, but I have issues with the table, how should I create the table? how can I make it create a new row and take the information from the textboxes to the right cells?

This is the code I`m trying to do:

public partial class WebForm1 : System.Web.UI.Page
{
    Table myTable;
    TableCell[] td;
    TableRow tr;
    protected void Page_Load(object sender, EventArgs e)
    {

        myTable = new Table();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        for (int i = 1; i <= 1;i++ )
        {
            tr = new TableRow();
            for(int j = 0; j <=3; j++)
            {
                td = new TableCell[3];
                td[0].Text = txtName.Text;
                td[1].Text = txtMail.Text;
                td[2].Text = txtAge.Text;

                tr.Cells.Add(td[j]);
            }
        }
            myTable.Rows.Add(tr);
            Panel1.Controls.Add(myTable);
    }

Break down the problem into smaller pieces.

1) Creating the table You said you already created "it". I don't know if you meant the text boxes or the table, but if you are already using design view, continue using it. There is a table control in the Toolbox you can drag onto the web page. Do that. Specify three columns and give them names.

2) Adding the inputs to the table Don't worry about new rows yet. Just focus on getting the values in the text boxes into the first row of the table. Double click the web page button. This should create code for a Click event. Here is where you add the code to copy the data from the boxes into the table. Something like :

`DataRow row = new DataRow();
row[0] = textboxName.Text;
row[1] = textboxEmail.Text;
row[2] = textboxAge.Text;
yourTable.Rows.Add(row);`

Obviously you want to replace the names here with whatever you named them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM