Surya sasidhar
Surya sasidhar

Reputation: 30343

Dynamic creating table?

I am writing a code for dynamically creating table in a button click, i Write the code it is working fine, But when i click the button again i want to add the rows to the existing table. Please help me. My code is :

Table table = new Table();
    table.ID = "Table1";
    Page.Form.Controls.Add(table);
    TableRow row = new TableRow();                       
    TableCell cell = new TableCell();
    TextBox tb = new TextBox();
    // Set a unique ID for each TextBox added
    tb.ID ="t1";
    tb.Text = "sasi";
    // Add the control to the TableCell
    cell.Controls.Add(tb);
    // Add the TableCell to the TableRow
    row.Cells.Add(cell);
    table.Rows.Add(row);

Upvotes: 0

Views: 223

Answers (1)

Tim B James
Tim B James

Reputation: 20374

On your button click, you will want to have the code

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

However, you will need to make sure that you are re-building the table on postback of the page, otherwise the FindControl will not find anything, and table will be null.

Upvotes: 1

Related Questions