Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > How to add a row to the table web control dynamically in asp .net 2.0 on a button click retaining the user input in previous rows?

Thread Tools

How to add a row to the table web control dynamically in asp .net 2.0 on a button click retaining the user input in previous rows?

Posts: n/a
Hi All,

I am very new to .net and trying to develop a small application.
I am using asp .net 2.0.

I have a table control (server) in my code that has header row like

<asp:Table id="Table1"
Runat="server" Height="1px" Width="523px"
<asp:TableRow runat="server" Height="21px">
<asp:TableCell runat="server" Width="105px">Cell
<asp:TableCell runat="server" Width="515px">Cell
<asp:TableCell runat="server" Width="232px">Cell

On page load event, I call "addRow" routine that adds a row to the
above table.
This row has totally 3 table cells in it, out of which the first 2
table cells contain "text box" in them and the last table cell contain
a "checkbox" in it.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
End If
End Sub

Sub addRow()
Dim numrows As Integer = 1
Dim numcells As Integer = 3
Dim j As Integer = Table1.Rows.Count + 1

Dim r As New TableRow()
Dim i As Integer
For i = 0 To numcells - 1
Dim c As New TableCell()
If i = 0 Or i = 1 Then
Dim txtBx As New TextBox()
txtBx.ID = "txtBx" & j & i
Dim chkBx As New CheckBox()
chkBx.ID = "chkBx" & j & i
End If
Next i
End Sub

Now, I have 2 buttons (web) on my page.
1) => "btnAdd" --> allows user to add more rows to the table.
2) => "btnDone" --> should display all the values that user has entered
in all the textboxes and checkboxes of the table "table1".

I have the following code

Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
Call addRow()
End Sub

Protected Sub btnDone_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnDone.Click

Dim m As Integer
Dim n As Integer
Dim cell1val As String = ""
Dim cell2val As String = ""
Dim cell3val As Boolean

For m = 1 To Table1.Rows.Count - 1
For n = 0 To 2
Select Case n
Case 0
Dim strID As String = "txtBx" & m & n
cell1val =
CType(Table1.Rows(m).Cells(n).FindControl(strID), TextBox).Text
Case 1
Dim strID As String = "txtBx" & m & n
cell2val =
CType(Table1.Rows(m).Cells(n).FindControl(strID), TextBox).Text
Case 2
Dim strID As String = "chkBx" & m & n
cell3val =
CType(Table1.Rows(m).Cells(n).FindControl(strID), CheckBox).Checked
End Select
response.write = cel1val & "<BR>" & "<BR>" & cell2val &
"<BR>" & cell3val
End Sub

My probelms here are:

1) when I click on "btnAdd" does not add any new row to the
table. My table seems to have 1 row always. Clicking on the "btnAdd"
does not increment the "table rows".

2) Since I failed to add more rows to the table, I tried to display
the values from the table by clicking "btnDone". After entering the
values to both the text boxes and check box...when I click on
"btnDone" table row (with text box, check box) disappears and
table reamins with the header row.
I want to retain the table with all the values that have been entered.

Can you guys let me know what I am missing?
Any help in this regard is greatly appreciated.


Reply With Quote
ort11 ort11 is offline
Junior Member
Join Date: Oct 2006
Posts: 2
Hi: having pretty much the same issue, have you resolved?
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Need to dynamically add <ASP:Button> controls to web user control page ASP .Net 1 09-02-2007 08:06 PM
Lexical array retaining elements from previous function invocations David McNerney Perl Misc 5 05-05-2006 12:47 AM
add row dynamically in a table which contains textfields,button. Javascript 6 03-19-2006 05:25 PM
How to add click for dynamically created User control page? ASP .Net 0 12-27-2005 06:50 AM