Clay Smith
Clay Smith

Reputation: 1079

.NET Master Page Session state variable not saving

I'm trying to store the session state in a master page to keep track of the previous URL. Here's what I'm doing

Public Property PreviousPage() As String
    Get
        Return Session("theprevpage")
    End Get
    Set(value As String)
        Session("theprevpage") = value
    End Set
End Property

Private Function HandleSiteNode(ByVal sender As Object, ByVal e As SiteMapResolveEventArgs) As SiteMapNode

    Dim currNode As SiteMapNode = SiteMap.CurrentNode.Clone(True)
    Dim tempNode As SiteMapNode = currNode
    Dim strPrev As String = PreviousPage

    ' Append parent pages query string back onto the parent's node URL
    If Not tempNode.ParentNode Is Nothing Then
        If strPrev.Contains("?") Then
            tempNode.ParentNode.Url = tempNode.ParentNode.Url + "?" + strPrev.Split("?")(1)
        End If
    End If

    Return currNode

End Function

And in the master page load function

    If Not IsPostBack Then

        AddHandler SiteMap.SiteMapResolve, AddressOf HandleSiteNode

        PreviousPage = Request.UrlReferrer.ToString()

    End If

Now, here is where it gets strange.

The first page is a login page the master load doesn't get called on. After I log in it then going to the main.aspx page, and it successfully saves the "login.aspx" page in the session state.

Now, when I go to navigate the 2nd time after logging in, the session state is set successfully, but by the time it gets into the HandleSiteNode which is called after the session was set successfully, the session still says the url is "login.aspx" and not "main.aspx"

No where else in the code am I setting this session state, it just seems to revert back to its previous value on its own.

No matter how many links I click & how many times the session is set, the Session variable will never change to anything else besides "login.aspx"

Help!

edit: Another odd detail, when I move the AddHandler line from master page into a non-master page, the session state is saved properly. However, if I try to move the Addhandler code into the Master page MainContent.Load function, it still doesn't work

Upvotes: 2

Views: 1882

Answers (2)

Clay Smith
Clay Smith

Reputation: 1079

Apparently, my web project has at least 2 different session states.

I can access the session state that contains the value I want by using

e.Context.Session("theprevpage")

This seems like a bit of a hack but it's working for me.

Upvotes: 1

ChrisLively
ChrisLively

Reputation: 88074

SiteMapResolve is a static event.

This means that it doesn't have access to the session object. You'll note this if you put a breakpoint in your HandleSiteNode code and inspect the Session.SessionId property.

The examples on MSDN about the event all target the global.asax file which means that handler is really geared towards a single use of the site. Note that the MSDN example I linked to is a little jacked in that it attaches a new event on every page load, which will eat memory. The event should only be attached to once.

Click here for more info on potential ways to get around the issues.

Upvotes: 1

Related Questions