JustBeingHelpful
JustBeingHelpful

Reputation: 18980

restoring session variable after clicking the browser [BACK] button

I have an issue with the browser [BACK] button. Let me explain.

There is a page called ManageClient.aspx. That page contains an address list of both [child] client addresses (child meaning the client you're viewing), and parent client addresses (clients the child inherits from). When someone clicks to view a client in the ManageClient.aspx page, there is no query string (but I did add a variable to fix my problem here). However, with some JavaScript, there is a session variable called Session("Id") that eventually gets set in the ManageClient.aspx page.

When the user clicks on an Address on the ManageClient.aspx page, they are taken to the AddressEditor.aspx page, and an "Id" variable in the query string is passed indicating the address id.

The hyperlinks in the address editor have been broken for quite some time. That is my goal--to fix them so the AddressEditor.aspx populates for the correct client. So to attempt to fix this, I added a query string variable to the ManageClient.aspx page called "Id". This "Id" variable indicates the client id of the client you're editing. Furthermore, I also added a query string variable to the AddressEditor.aspx page called "ClientId". Because the client AddressEditor is in the same Wizard workflow as the ManageClient.aspx page, I needed to set that "ClientId" query string parameter so when I reach the AddressEditor.aspx page, I then can set the Session("ClientId") session variable so that if the user continues in the wizard for that client, they are in the correct context. This works great.

What doesn't work is when the user enters the ManageClient.aspx page for the [child] client, and then clicks on a parent client address (sending them to AddressEditor.aspx in the context of the parent client), and then clicks the [BACK] button (sending them back to the ManageClient.aspx page). I want them to be back to the context of the child client, not the parent client. I put a breakpoint in the ManageClient.aspx Page_PreRender event, and it does in fact get called. But when the page renders, the page populates with the parent client, not the child client. But if I highlight the URL bar, and hit [ENTER], the page renders as a child client (AWESOME) so it must have populated my session variable as a child client correctly (but rendered incorrectly), but I want it to do this when they clicked the [BACK] button. How do I fix the ManageClient.aspx page for this workflow I just described?

Here is the Page_PreRender event:

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender

    Dim clientId As Integer = CType(Request.QueryString("ClientId").ToString, Integer)
    If clientId > 0 Then
        Session("ClientId") = clientId
        Me.LIMSClientId = clientId
    End If

End Sub

Upvotes: 1

Views: 1658

Answers (1)

JustBeingHelpful
JustBeingHelpful

Reputation: 18980

This was very simple. The lists in the page were being built in Page_Load which was called after Page_PreRender--where the session variable was being set. I just had to place the code I had in that Page_PreRender event above the code where the data lists in the page were being built.

Upvotes: 1

Related Questions