2
我想动态地将行添加到ASP.NET中的System.Web.UI.WebControls.Table控件。我发现了一些sample code,这是我正在寻找,但它只增加了一行。将行动态添加到ASP.NET中的表?
看起来像表行数不会从一个页面加载到下一个。用户应该能够根据需要添加任意数量的行,并且我需要从每行中捕获数据。
我错过了什么?下面的代码。
<%@ 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>
谢谢,这是有帮助的,但我还没有“声誉”投票的答案。 :)我希望有一个简单的方法来说,“维护此表中的行数和回发后的数据。”某种类型的配置选项,如AutoEventWireup = True,尽管我意识到这涉及到事件处理程序。我用这个非常简单的页面。 – jtherkel 2009-12-23 21:09:05