Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Table row creation dynamically

Status
Not open for further replies.

TPetersonFlorida

Programmer
Aug 8, 2003
76
US
I have a web form with a button on it. I have a table with one row already pre-defined. What i want to happen is the user clicks the button to append another row onto the table, in essence have the ability to append as many rows as they would like. I can get it to create one row but after that all subsequent button clicks are overwriting the row already created. any ideas?

my code behind the button:

Private Sub btnAddRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddRow.Click
Dim myCell As New TableCell()
Dim myRow As New TableRow()

myCell.Controls.Add(New LiteralControl(Date.Now))
myCell.BorderStyle = BorderStyle.Solid
myCell.BorderWidth = Unit.Pixel(1)
myRow.Cells.Add(myCell)
myTable.Rows.Add(myRow)

End Sub

my html code for the page:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="DynamicTable.aspx.vb" Inherits="Ecore.DynamicTable"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>DynamicTable</title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content=" </HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Button id="btnAddRow" style="Z-INDEX: 101; LEFT: 11px; POSITION: absolute; TOP: 6px" runat="server" Text="Add Row"></asp:Button>
<asp:Label id="lblCount" style="Z-INDEX: 102; LEFT: 112px; POSITION: absolute; TOP: 9px" runat="server">Label</asp:Label>
<asp:Table id="myTable" style="Z-INDEX: 103; LEFT: 17px; POSITION: absolute; TOP: 46px" runat="server" Width="445px"></asp:Table>
</form>
</body>
</HTML>
 
You would think that the rows would get held in ViewState for the Table... Just to be sure - make sure your table's EnableviewState property is set to true. If that doesn't work, how does this sound:

You could try saving the rows in collection which is held in ViewState. Each time the btnAddRow is clicked, add a new row to the collection and then add each row in the collection to the table
Code:
Private Sub btnAddRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddRow.Click
        
    Dim addedRows as ArrayList
    if not ViewState("AddedRows") is nothing Then
        addedRows = CType(ViewState("AddedRows"), ArrayList)
    Else
        addedRows =  New ArrayList()
    End If
  
    Dim myCell As New TableCell()
    Dim myRow As New TableRow()

    myCell.Controls.Add(New LiteralControl(Date.Now))
    myCell.BorderStyle = BorderStyle.Solid
    myCell.BorderWidth = Unit.Pixel(1)
    myRow.Cells.Add(myCell)

    addedRows.Add(myRow)
    ViewState("AddedRows") = addedRows

    Dim savedRow as TableRow
    For Each savedRow in addedRows
        myTable.Rows.Add(savedRow)
    End For
    

End Sub



Greetings,
Dragonwell
 
What's going on is that for dynamically created controls, you need to re-build them with the same ID on every request if you want them to keep showing up. They will pick their own ViewState up once they are built properly.

Try something like:
Code:
'click event handler
Dim rowCount as Integer

'keep track of rows
If ViewState("RowCount") <> null Then
   rowCount = CType( ViewState("RowCount"), Integer )
Else
   rowCount = 0
End If

rowCount = rowCount + 1

'make "rowCount" number of rows in a for loop

ViewState("RowCount") = rowCount
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top