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

Discussion in 'ASP .Net' started by loga123, May 19, 2006.

  1. loga123

    loga123 Guest

    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
     
    loga123, May 19, 2006
    #1
    1. Advertising

  2. loga123

    ort11

    Joined:
    Oct 3, 2006
    Messages:
    2
    Hi: having pretty much the same issue, have you resolved?
     
    ort11, Oct 3, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Purvi T
    Replies:
    0
    Views:
    936
    Purvi T
    Oct 19, 2004
  2. Amy
    Replies:
    0
    Views:
    1,041
  3. phl
    Replies:
    1
    Views:
    4,442
    Martin Jay
    Jun 8, 2006
  4. Replies:
    1
    Views:
    1,214
    Nathan Sokalski
    Sep 2, 2007
  5. Replies:
    6
    Views:
    175
    Dr John Stockton
    Mar 19, 2006
Loading...

Share This Page