我想通过单击按钮将行添加到表格控件。我尝试使用下面的代码但出现错误,有人可以帮忙吗?
protected void btn_Click(object sender, EventArgs e)
{
//Table table = (Table)Page.FindControl("tblName");
TableRow tr = new TableRow();
tr.Cells.Add(new TableCell());
tr.Cells.Add(new TableCell());
tr.Cells.Add(new TableCell());
tr.Cells[0].Text = TextBox1.Text;
tr.Cells[1].Text = TextBox2.Text;
tr.Cells[2].Text = TextBox3.Text;
//add the row to the existing table.
this.tblName.Rows.Add(tr);
//this.tblName.Rows.Add();
}
-----------------asp.net-------------
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="btn_Click" />
<table id="tblName" Runat="server" style="width:100%;">
<tr>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td>
</tr>
</table>
<br />
</div>
</form>