简体   繁体   中英

How can I achieve two different sessions for the same page?

Is it possible to have unique sessions for the same page in different tabs? Lets say I have in one tab this url https://www.test.com/test.aspx?id=123 and another tab with https://www.test.com/test.aspx?id=124 . Each one has data loaded for their respective id. If I do something in the first tab that saves a value in a Session and then do the same with the second tab, if I switch to the first tab and make a PostBack (like calling an event that saves the information), the Session value from the second tab will be saved on the first. How can I avoid that?

This is a big pain - and one that I wish a built in option/setting was available.

However, what often works VERY well?

You can use session to pass value(s) to a page, but ONCE you get on that page and the first page load (Is PostBack = False), then transfer/save the few passed values into ViewState. ViewState is quite much as easy and flexible as Session(), and it is a persisting set of values that is per page, and per tab. And it qiute much is the same if you used a bunch of hidden field controls for this anyway.

So, don't use the session() for persisting of values on a page, but use session() to pass values.

So now, you can have multiple pages open - and you only grabbed/used the session() to get values on first page load - after that, you code using ViewState. Now you want to be careful, since viewstate is browser side based - and you don't want to overload it. (put much data in Viewstate - since whatever you have in ViewState WILL MAKE round trips and lives in the browser - every post back will send this data from client side to server.

ViewState is also how asp.net manages all your controls. So if you have a bunch of text boxes - and do a post-back? you note that these controls values persist automatic for you. This works for you by asp.net using Viewstate. And Even if you have two of the same pages loaded - you note that text boxes etc. still persist for each of the pages.

Session() = for global types of values for that user - applies to all pages.

ViewState() = for per page values.

So, if you use session() to pass values to that page - say a new tab, then on first page load - transfer the few values to ViewState. Those values now are local to the one page, and are not effected by other tabs/pages open - even when pages are on the same page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM