单击按钮时,我想在其中添加一个新单元格和一个 TextBox。但是当我单击当前行中的按钮时,它的单元格和 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();
}