Add rows with TextBox in cells to existing table. Maintain viewstate.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bgernon
    New Member
    • Aug 2008
    • 1

    Add rows with TextBox in cells to existing table. Maintain viewstate.

    I have a table that consists of two rows with three cells each row. The cells contain textboxes. I am able to successfully add a new row with textboxes when a button is clicked. The problem is retaining the newly added row with the filled in textboxes on the next button click. I think I am handling my viewstate incorrectly.

    This code adds the new row:
    Code:
    Private Sub addRowsInTable()
            Dim TextID As Integer
            Dim TextIDCount As String
            TextID = Int32.Parse(ViewState("rows").ToString()) + 3
            TextIDCount = TextID.ToString
            Dim text As New TextBox()
            'Text.AutoPostBack = False
            Dim cell As New HtmlTableCell()
            Dim row As New HtmlTableRow()
    
            text.ID = "TxtWhat" + TextIDCount
            text.Attributes.Add("style", "width:371px")
            cell.Controls.Add(text)
            row.Cells.Add(cell)
    
            text = New TextBox()
            cell = New HtmlTableCell()
            text.ID = "TxtWho" + TextIDCount
            text.Attributes.Add("style", "width:216px")
            cell.Controls.Add(text)
            row.Cells.Add(cell)
    
            text = New TextBox()
            cell = New HtmlTableCell()
            text.ID = "TxtWhen" + TextIDCount
            cell.Controls.Add(text)
            row.Cells.Add(cell)        
            tblReasons.Rows.Add(row)
            ViewState("Rows") = (Int32.Parse(ViewState("rows").ToString()) + 1).ToString
    
        End Sub
    
    This is where I think I'm messing up.  ViewState("rows") shows as 0 when I do a step through.  It is not retaining the value from the above sub.
    
    Protected Overrides Sub LoadViewState(ByVal savedState As Object)
            MyBase.LoadViewState(savedState)
            myRowcount = Int32.Parse(ViewState("rows").ToString())
    
            For i As Integer = 1 To myRowcount
                Dim text As New TextBox()
                Dim cell As New HtmlTableCell()
                Dim row As New HtmlTableRow()
    
                text.ID = "TxtWhat" + (i + 3).ToString  
                text.Attributes.Add("style", "width:371px")
                cell.Controls.Add(text)
                row.Cells.Add(cell)
    
                text = New TextBox
                cell = New HtmlTableCell()
                text.ID = "TxtWho" + (i + 3).ToString
                text.Attributes.Add("style", "width:216px")
                cell.Controls.Add(text)
                row.Cells.Add(cell)
    
                text = New TextBox
                cell = New HtmlTableCell()
                text.ID = "TxtWhen" + (i + 3).ToString
                cell.Controls.Add(text)
                row.Cells.Add(cell)
    
                tblReasons.Rows.Add(row)
            Next
    End Sub
    Thanks
    Last edited by DrBunchman; Aug 13 '08, 06:50 AM. Reason: Added [Code] Tags - Please use the '#' button
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    If you declare your textboxes outside of that function as Private members to your page...and initialize those textboxes in your Page_Init event you should be able to maintain the text in the textboxes.

    Please check out the article on how to use dynamic controls in asp.net.

    Comment

    Working...