单击按钮时,我想添加一个新单元格和一个文本框。但是当我单击当前行中的按钮时,它的单元格和TextBox将被删除,并添加新的单元格。单击按钮时如何将文本框添加到表格
标记:
<form id="form1" runat="server">
<div>
<asp:Panel ID="pnlForTextBoxes" runat="server">
</asp:Panel>
<asp:DropDownList ID="ddlNumbers" runat="server" Height="16px" Width="143px">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
</asp:DropDownList>`enter code here`
<asp:Table ID="myActualTable" runat="server">
</asp:Table>
<br />
<asp:Button ID="cmdAddTextBox" runat="server" Text="Add" OnClick="cmdAddTextBox_Click" />
</div>
</form>
代码:
protected void cmdAddTextBox_Click(object sender, EventArgs e)
{
TableRow myRow = new TableRow();
myRow.ID = "row" + ddlNumbers.SelectedValue.ToString();
TableCell myCell = new TableCell();
myCell.ID = "cell" + ddlNumbers.SelectedValue.ToString();
TextBox myTextBox = new TextBox();
myTextBox.ID = "txt" + ddlNumbers.SelectedValue.ToString();
myTextBox.Text = "TextBox number " + ddlNumbers.SelectedValue.ToString();
myCell.Controls.Add(myTextBox);
myRow.Cells.Add(myCell);
myActualTable.Rows.Add(myRow);
myTextBox.Dispose();
myCell.Dispose();
myRow.Dispose();
}
我想你可以让你的生活更容易,如果你想通过像[DataGrid](http://msdn.microsoft.com/en-us/library/system.web.ui .webcontrols.datagrid)或[GridView](http://msdn.microsoft.com/zh-cn/library/4w7ya1ts),它们提供了用于添加,删除和编辑行的即用功能。 – Filburt