I'm storing some value in an asp.net session on the first page. On the next page, this session value is being read. However if multiple tabs are opened and there are multiple page 1->page 2 navigation going on, the value stored in session gets mixed up since the session is shared between the browser tabs.
I'm wondering what are the options around this :
Query String: Passing value between the pages using query string, I don't want to take this approach since there can be multiple anchor tags on page 1 linking to page 2 and I can not rewrite the URLs of each tag since they are dynamic.
Cookies??? In-memory cookies are shared across browser tabs too, same as the session cookie, rite ?
Any other option?
PS: Page 1 to page 2 is not a form submit.
When you open the same screens in different browsers (eg. 1 tab and a incognito tab, or chrome and firefox) you must have new session-variables. When you're open tabs in the same browser they share the same session.
You cannot (and should not) do that. User can always just open use another browser or another computer to open another view onto the web site. So, basically you cannot ever prevent this. Your web-site should be able to handle multiple tabs viewing the same state.
You can put in an hidden input in the page a value like the timestamp generated in the back-end when the page is requested. In this way every page will has a unique value. Then you can put a hashmap in session where the key - value pairs are the generated timestamp (key) and the object you need (value).
I figured out a solution :
Using Javascript assign a unique id like a guid to the browser window / tab by assigning the guid value to the window.name property. window.name property is unique to each browser window/tab and won't be shared across the windows.
Using the guid as the key, read and write data to your ASP.NET session via a webservice. Since javascript does not have access to asp.net session, you will need to use a webservice and call it's method through javascript.
The data can be transfered between javascript and webservice via JSON.
This is similar to what ViewState does automatically.
EDIT
Comment response
I don't think that's possible as you described it.
In your question you mentioned page 1 to 2 navigation. If the user types in the URL for page 2 in the browser then how do you tie that current page 2 rendering with the previous page 1?
You need something to do so, unless the code-behind can search a list of incomplete workflows that have stopped at page 1 and uses that as the previous guid value for the new page 2 rendering.
Comment response 2
You can find all the links on the page and modify those but it hints that something is wrong ( in my opinion ). But...
http://www.extremeexperts.com/Net/Articles/LoopingthroughControls.aspx
Gives you a simple way of processing all controls on the page. You can test to see if the control is a hyperlink easily. Not that all links will need that runat="server" parameter set for this to work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With