I’ve got an index page which sends users to an edit products page on separate browser tabs.
For each products edited the index rewrites the Session[“ProductID”].
The Edit page then has the following code to have a unique identifier for this tab and product:
if (!IsPostBack) //first time page load
{
Random R = new Random(DateTime.Now.Millisecond + DateTime.Now.Second * 1000 + DateTime.Now.Minute * 60000 + DateTime.Now.Minute * 3600000);
PageID.Value = R.Next().ToString();
Session[PageID.Value + "ProductID"] = Session["ProductID"];
}
This works, and when the same user opens multiple tabs I only reference the Session[PageID.Value + “ProductID”] in my code so that I always have the proper ID. (I’m working in a trusted environment this is for an intranet, therefore I’m not too bothered with the level of security).
My issue occurs if the user does a page refresh by hitting the F5 key. At which point the Session[PageID.Value + “ProductID”] gets the Session[“ProductID”] of the last product he opened.
For example:
User 1 opens product1 in tab1
User 1 opens product2 in tab2
Whenever they use the tool normally, everything works fine. However if:
User 1 on product1 page hits the refresh button (F5) the product1 page becomes product2 page
Is there a way to detect a page refresh from a “first load/redirect from another page” so that I can then tell my page not to update my Session[PageID.Value + “ProductID”]?
I’ve solved a very similar issue by storing two versions of a state-identifying parameter: one in Session and one in either the ViewState or the URL (QueryString).
If you compare the two values on Page_Load, that will tell you whether the session variable has changed since the page was first loaded. This should be just what you need.
EDIT: Rough sketch of the code (warning – haven’t seen the actual code since I wrote it 3 years ago):
In the above code, note that changes to the ViewState (including the value of a Hidden control) will only take effect on the next PostBack. On a refresh, they’ll revert to their most recent value. In my case, that was what I wanted, but it sounds like it’s not quite right for your situation. Still, that information could be useful to you, depending on how you implement this.
I’ve left out a discussion of comparing
currentProductIDtoSession[PageID.Value + "ProductID"], since I’ve already posted a lot of code, and I don’t know the details of what you’re trying to do. But there are various ways you can use Session, ViewState, and the QueryString to glean information about the state and history of the page.Hopefully that should give you the general idea. Let me know if that’s not enough to get you going.