How to add a row to the table web control dynamically in asp .net 2.0 on a button click retaining th

L

loga123

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
this.

<asp:Table id="Table1"
GridLines="Both"
HorizontalAlign="Center"
Font-Name="Verdana"
Font-Size="8pt"
CellPadding="0"
CellSpacing="0"
Runat="server" Height="1px" Width="523px"
BackColor="PaleTurquoise"
BorderStyle="Groove">
<asp:TableRow runat="server" Height="21px">
<asp:TableCell runat="server" Width="105px">Cell
1</asp:TableCell>
<asp:TableCell runat="server" Width="515px">Cell
2</asp:TableCell>
<asp:TableCell runat="server" Width="232px">Cell
3</asp:TableCell>
</asp:TableRow>
</asp:Table>

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
addRow()
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
c.Controls.Add(txtBx)
Else
Dim chkBx As New CheckBox()
chkBx.ID = "chkBx" & j & i
c.Controls.Add(chkBx)
End If
r.Cells.Add(c)
Next i
Table1.Rows.Add(r)
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
Next
response.write = cel1val & "<BR>" & "<BR>" & cell2val &
"<BR>" & cell3val
Next
End Sub

My probelms here are:

1) when I click on "btnAdd".....it 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"....my 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.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top