1 Attachment(s)
Dynamically adding table rows to a asp.net table.
HI guys,
I'm trying to add new rows with cells that have text boxes in them. What I want is to have a table that the user enter's numbers into the text boxes think clicks on the button. A new rows appears so that they can add another ticker. But this is what I'm getting. when i add one row of data I click the button and a new row appears but the data shows up on the first row. They need to stay on each row. I'm not saving the data anyway. I do have viewstate to keep track of the rows but not sure what I'm doing wrong.
Code:
<asp:Table ID="Table1" runat="server" Caption="Portfolio" Width="1001px">
<asp:TableRow runat="server">
<asp:TableCell runat="server">Ticker</asp:TableCell>
<asp:TableCell runat="server"># Shares</asp:TableCell>
<asp:TableCell runat="server">Price</asp:TableCell>
<asp:TableCell runat="server">Buy Date</asp:TableCell>
<asp:TableCell runat="server">Sell Date</asp:TableCell>
<asp:TableCell runat="server">$ Gain</asp:TableCell>
<asp:TableCell runat="server">% Gain</asp:TableCell>
</asp:TableRow>
<asp:TableRow runat="server">
<asp:TableCell runat="server">
<asp:TextBox ID="j0i0" runat="server"></asp:TextBox></asp:TableCell>
<asp:TableCell runat="server"><asp:TextBox ID="j0i1" runat="server"></asp:TextBox></asp:TableCell>
<asp:TableCell runat="server"><asp:TextBox ID="j0i2" runat="server"></asp:TextBox></asp:TableCell>
<asp:TableCell runat="server"><asp:TextBox ID="j0i3" runat="server"></asp:TextBox></asp:TableCell>
<asp:TableCell runat="server"><asp:TextBox ID="j0i4" runat="server"></asp:TextBox></asp:TableCell>
</asp:TableRow>
</asp:Table>
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
numrows = CType(ViewState("nums"), Integer) + 1
addrows(numrows)
ViewState("nums") = numrows
End Sub
Sub addrows(ByVal rows As Integer)
Dim j As Integer = rows + 1
For j = 0 To rows - 1 Step 1
Dim arow As New TableRow
For i = 0 To 4 Step 1
Dim tb As New TextBox
Dim tc As New TableCell
tb.ID = "j" & j & "i" & i
tc.Controls.Add(tb)
arow.Cells.Add(tc)
Next
Table1.Rows.Add(arow)
Next
End Sub
Any idea's on how to add rows to a table?