Dynamically adding rows to table web server control

Discussion in 'ASP .Net' started by Geary, Jul 24, 2007.

  1. Geary

    Geary Guest

    I am trying to programmatically add rows of data to a table web control
    in VS2005. I am getting the data from 2 drop-down controls and 2
    text-boxes. The code below is executed when I click on the "add"
    button. The code is pretty much lifted from an example on MSDN.

    Problem:

    The first time I try to add a row, it works fine. But when I try to add
    another row, it simply overwrites the first. I need to add new row
    every time the user clicks the 'add' button.

    --------------------------------

    Protected Sub btnProdAdd_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles btnProdAdd.Click

    Dim tr As New TableRow()

    Dim cellGrade As New TableCell()
    cellGrade.Text = cboGrade.SelectedValue.ToString
    tr.Cells.Add(cellGrade)

    Dim cellStrips As New TableCell()
    cellStrips.Text = cboStrips.SelectedValue.ToString
    tr.Cells.Add(cellStrips)

    Dim cellYards As New TableCell()
    cellYards.Text = txtYards.Text.ToString
    tr.Cells.Add(cellYards)

    Dim cellScrap As New TableCell()
    cellScrap.Text = txtScrap.Text.ToString
    tr.Cells.Add(cellScrap)

    tblProd.Rows.Add(tr)


    End Sub
    Geary, Jul 24, 2007
    #1
    1. Advertising

  2. Geary

    bruce barker Guest

    your code needs to remember the rows it added on the last click, and
    re-add them on the postback (say in oninit).

    -- bruce (sqlwork.com)

    Geary wrote:
    > I am trying to programmatically add rows of data to a table web control
    > in VS2005. I am getting the data from 2 drop-down controls and 2
    > text-boxes. The code below is executed when I click on the "add"
    > button. The code is pretty much lifted from an example on MSDN.
    >
    > Problem:
    >
    > The first time I try to add a row, it works fine. But when I try to add
    > another row, it simply overwrites the first. I need to add new row
    > every time the user clicks the 'add' button.
    >
    > --------------------------------
    >
    > Protected Sub btnProdAdd_Click(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles btnProdAdd.Click
    >
    > Dim tr As New TableRow()
    >
    > Dim cellGrade As New TableCell()
    > cellGrade.Text = cboGrade.SelectedValue.ToString
    > tr.Cells.Add(cellGrade)
    >
    > Dim cellStrips As New TableCell()
    > cellStrips.Text = cboStrips.SelectedValue.ToString
    > tr.Cells.Add(cellStrips)
    >
    > Dim cellYards As New TableCell()
    > cellYards.Text = txtYards.Text.ToString
    > tr.Cells.Add(cellYards)
    >
    > Dim cellScrap As New TableCell()
    > cellScrap.Text = txtScrap.Text.ToString
    > tr.Cells.Add(cellScrap)
    >
    > tblProd.Rows.Add(tr)
    >
    >
    > End Sub
    bruce barker, Jul 24, 2007
    #2
    1. Advertising

  3. Geary

    Geary Guest

    If I am understanding you correctly, you are saying that every time the
    user triggers the code to add a new record to the table control, the
    code has to re-add all of the existing records already in the table in
    addition to the new one?

    And this would be because of the statelessness of web apps?

    bruce barker wrote:
    > your code needs to remember the rows it added on the last click, and
    > re-add them on the postback (say in oninit).
    >
    > -- bruce (sqlwork.com)
    >
    > Geary wrote:
    >> I am trying to programmatically add rows of data to a table web
    >> control in VS2005. I am getting the data from 2 drop-down controls
    >> and 2 text-boxes. The code below is executed when I click on the
    >> "add" button. The code is pretty much lifted from an example on MSDN.
    >>
    >> Problem:
    >>
    >> The first time I try to add a row, it works fine. But when I try to
    >> add another row, it simply overwrites the first. I need to add new
    >> row every time the user clicks the 'add' button.
    >>
    >> --------------------------------
    >>
    >> Protected Sub btnProdAdd_Click(ByVal sender As Object, ByVal e As
    >> System.EventArgs) Handles btnProdAdd.Click
    >>
    >> Dim tr As New TableRow()
    >>
    >> Dim cellGrade As New TableCell()
    >> cellGrade.Text = cboGrade.SelectedValue.ToString
    >> tr.Cells.Add(cellGrade)
    >>
    >> Dim cellStrips As New TableCell()
    >> cellStrips.Text = cboStrips.SelectedValue.ToString
    >> tr.Cells.Add(cellStrips)
    >>
    >> Dim cellYards As New TableCell()
    >> cellYards.Text = txtYards.Text.ToString
    >> tr.Cells.Add(cellYards)
    >>
    >> Dim cellScrap As New TableCell()
    >> cellScrap.Text = txtScrap.Text.ToString
    >> tr.Cells.Add(cellScrap)
    >>
    >> tblProd.Rows.Add(tr)
    >>
    >>
    >> End Sub
    Geary, Jul 26, 2007
    #3
    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. Rob Meade
    Replies:
    4
    Views:
    7,297
    Rob Meade
    Nov 25, 2003
  2. lucky
    Replies:
    0
    Views:
    511
    lucky
    Jan 12, 2005
  3. Valli
    Replies:
    3
    Views:
    821
    Valli
    Sep 21, 2007
  4. Komalid
    Replies:
    0
    Views:
    2,124
    Komalid
    Dec 21, 2007
  5. T.G.
    Replies:
    2
    Views:
    1,067
Loading...

Share This Page