我正在尝试将行动态添加到 ASP.NET 中的 System.Web.UI.WebControls.Table 控件。我找到了一些示例代码,可以满足我的要求,但它只添加了一行。
似乎表格行数不会从一个页面加载到下一个页面。用户应该能够根据需要添加尽可能多的行,我需要从每一行中捕获数据。
我错过了什么?代码如下。
<%@ Page Language="VB" %>
<script runat="server">
Sub btnAddEmail_Click(ByVal Sender As Object, ByVal e As EventArgs)
Dim tr As New TableRow
Dim tcLabel As New TableCell
Dim tcTextBox As New TableCell
Dim newLabel As New Label
Dim newTextBox As New TextBox
Dim i As Integer = Table1.Rows.Count + 1
newLabel.ID = "Email" + Convert.ToString(i)
newLabel.Text = "Email" + Convert.ToString(i)
newTextBox.ID = "txtEmail" + Convert.ToString(i)
tcLabel.Controls.Add(newLabel)
tcTextBox.Controls.Add(newTextBox)
tr.Cells.Add(tcLabel)
tr.Cells.Add(tcTextBox)
Table1.Rows.Add(tr)
End Sub
</script>
<form id="form1" runat="server">
<div>
<asp:Button runat="server" Text="Add Email" ID="btnAddEmail"
onclick="btnAddEmail_Click" />
<asp:Table ID="Table1" runat="server">
<asp:TableRow>
<asp:TableCell>
<asp:Label ID="Email1" runat="server" Text="Email1"></asp:Label></asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtEmail1" runat="server"></asp:TextBox></asp:TableCell>
</asp:TableRow>
</asp:Table>
</div>
</form>