2011-08-27 120 views
0

我正在寫一個代碼動態創建一個按鈕點擊表,我寫它的代碼工作正常,但是當我再次單擊按鈕我想要添加行到現有的表。請幫幫我。我的代碼是:動態創建表?

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); 

回答

1

在您按一下按鈕,你會希望有代碼

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

但是,你需要確保你重新建設上的回發的表頁面,否則FindControl將不會找到任何內容,並且table將爲空。