jason
jason

Reputation: 3615

Asp.net session variables get mixed up

I have a user control that uses an UpdatePanel. The controls is essentially a table with a Button. When the user clicks the Button, a modal popup opens that allows them to select some value. Data for the table (which uses a Repeater as its DataSource) is stored in a session variable between partial postbacks (when the UpdatePanel fires) as a list of objects. Everything works fine if I have just one control but if I use this control in the same page more than once, the list of objects in the session variables get combined and are not separated for each control. I thought this might be because the session variable names are not unique, so anywhere I call or use the variable, I do it like this:

Dim sessionName as string = Me.UniqueID & "_" & "userNotificationDS"
Session(sessionName) = myListOfObjects

But this has not changed the outcome. Anyone know what I might be doing wrong here? If you believe the full code would be helpful, let me know.

Control Server Code:

Protected Sub delete_click(ByVal sender As Object, ByVal e As EventArgs)
    Dim btn As LinkButton = CType(sender, LinkButton)
    Dim ds As New List(Of myObject)
    sessionName = Me.UniqueID & "_" & "myDataSet"

    ds = Session(sessionName.ToString)

    Dim id As String = btn.CommandArgument

    ds.RemoveAll(Function(userNotification) userNotification.User.NetworkID.Equals(id))

    Session(sessionName.ToString) = ds


    bindData(ds)
End Sub


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    sessionName = Me.UniqueID & "_" & "myDataSet"

    If (Session(sessionName.ToString) IsNot Nothing) Then
        bindData(Session(sessionName.ToString))
    End If
End Sub  

 Private Function buildPagedSet(ByVal userNotification As List(Of myObject)) As PagedDataSource
    Dim ps As PagedDataSource = New PagedDataSource()

    ps.DataSource = userNotification
    ps.AllowPaging = True
    ps.PageSize = numRows

    Return ps
End Function

  Public Sub bindData(ByVal commentList As List(Of myObject))
    sessionName = Me.UniqueID & "_" & "myDataSet"
    Dim currentPage As Integer = 0
    Dim ps As PagedDataSource
    Dim numLable As Label
    Dim denomLable As Label
    Dim curPage As Integer = 1
    Dim totalPage As Integer = 0

    If (Not myObject Is Nothing) Then

        Try
            ps = buildPagedSet(commentList)
            totalPage = ps.PageCount
            Session(sessionName.ToString) = commentList 
            rowTotal = ps.Count

            'for paging
            If Not (ViewState(Me.UniqueID & "_Page") Is Nothing) Then
                currentPage = Convert.ToInt32(ViewState(Me.UniqueID & "_Page"))
            Else
                ViewState(Me.UniqueID & "_Page") = 1
                currentPage = 1
            End If

            If (currentPage > 0 And currentPage <= ps.PageCount) Then
                ps.CurrentPageIndex = currentPage - 1
                Me.dataRepeateUsers.DataSource = ps
                Me.dataRepeateUsers.DataBind()

            ElseIf (currentPage >= ps.PageCount) Then
                ViewState(Me.UniqueID & "_Page") = Convert.ToInt32(ViewState(Me.UniqueID & "_Page")) - 1
            ElseIf (currentPage <= 0) Then
                ViewState(Me.UniqueID & "_Page") = Convert.ToInt32(ViewState(Me.UniqueID & "_Page")) + 1
            Else
            End If

        Catch ex As Exception
            Throw
        End Try
    Else
        Dim emptySet As New List(Of myObject)
        Me.dataRepeateUsers.DataSource = emptySet
        Me.dataRepeateUsers.DataBind()
    End If

End Sub

The control is instantiated like this:

Me.notifier1.bindData(notificationList)

In this example, when the user deletes something from notifier1 ( the delete_click event) the object is removed from the list and it gets added back to the session. If anything causes notifier2's update panel to fire, it will display the same exact data as notifier1

Upvotes: 2

Views: 1274

Answers (2)

Andrew Morton
Andrew Morton

Reputation: 25027

If Kelsey's hunch is correct, you could store each of the instances of myListOfObjects in a Dictionary(Of String, myListOfObjectsType) and use the .UniqueID as the key (the Of String part).

Upvotes: 0

Kelsey
Kelsey

Reputation: 47736

My hunch is your are storing your myListOfObjects in the session but reusing that object somwhere and modifying it and storing it in the Session again with a different key. It is probably still the same object being stored for both Session keys.

You can do a simple test by storing an object in the Session with two differnt keys. Then pull it out using the first key and modify that object and don't reassign it back to the Session (not needed anyways). Now pull the other object out from the second key and look at it. It will match the modifications because the object is the same object just stored under two diffeent keys in the Session.

Upvotes: 2

Related Questions