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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Maintain User Control State

Status
Not open for further replies.

chadau

Programmer
Nov 14, 2002
155
US
I want to be able to add a usercontrol on the fly. The code below adds the usercontrol to the Web page. The problem is that each time I add one the previous one is lost. Is there any way I can fix this problem?

Code:
Private Sub AddESOItem()
        Dim tCell As TableCell
        Dim tRow As TableRow
        Dim ucESOitem As ESOItem
        Dim i As Int16 = (Me.Session.Item("ESOItemNo"))

        ucESOitem = New ESOItem
        ucESOitem = CType(LoadControl("ESOItem.ascx"), ESOItem)
        tCell = New TableCell
        tRow = New TableRow
        ucESOitem.ID = "ESOItem" & i
        tCell.Controls.Add(ucESOitem)
        tRow.Cells.Add(tCell)
        Me.tblDetails.Rows.Add(tRow)
        Me.Session.Item("ESOItemNo") = Me.Session.Item("ESOItemNo") + 1
    End Sub

 
Looks as if your session key is being overriden with the new value; therefore losing any reference to an earlier value.

regards,
Brian
The problem with doing something right the first time is that nobody appreciates how difficult it was.
 
The probelm may be with your session value. I would put in a break point within your routine to check the value of i everytime you are attempting to add a control, it may be that you are adding the same control multiple times.

Try this:
Code:
Private Sub AddESOItem()
        Dim tCell As New TableCell ()
        Dim tRow As New TableRow ()
        Dim ucESOitem As New ESOItem ()
        Dim i As Int16 = cint(Me.Session.Item("ESOItemNo"))
        ucESOitem = CType(LoadControl("ESOItem.ascx"), ESOItem)

        ucESOitem.ID = ("ESOItem" & i.ToString())
        tCell.Controls.Add(ucESOitem)
        tRow.Cells.Add(tCell)
        Me.tblDetails.Rows.Add(tRow)
        i = i + 1
        Me.Session.Item("ESOItemNo") =  i.ToString() 
End Sub

If this does not solve the problem code the code that calls AddESOItem()?

Regards,
Natgreen
 
Whenever the page reloads upon clicking the link to add another user control it looses the existing user control for some reason. All of the other asp.net controls maintain state, why doesn't the user control?

Code:
 Private Sub lbtnAddItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtnAddItem.Click
        Me.AddESOItem()
    End Sub
 
what do you mean by "All of the other asp.net controls maintain state, why doesn't the user control?" are the other controls added dynamically also to tblDetails or just the user control?

It seems as if the problem may be with how you add the user controls to the page, not the user controls themselves. It looks like on postback you are loosing your previously added control in tblDetails.

Check out this article:
 
NatGreen, you are absolutely correct in your assumption that I am loosing the user controls on page load. Is there an easier way of accomplishing what I am trying to do here?
 
Based on the looks of how you have your session variables named, you are writing over the existing contents of your session variable. Try appending to the table, then adding the entire table to session. This way, you can get your table from session, add new item, then re-add to session.

regards,
Brian
The problem with doing something right the first time is that nobody appreciates how difficult it was.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top