Solved

How to clone a session variable when openning a new window in the C# ASP.net?

Posted on 2004-04-15
10
866 Views
Last Modified: 2012-06-27

Let say i have 2 session variables: Val1, Val2; and 2 pages: page1, page2 (Page2 is opened when clicking on the link on the page1).

I want that the Val1 is shared between 2 pages but Val2 is separated. It means that if the page2's Val2 is changed in the page2 will not effect the page1's Val2. At the begining, the value of the Val2 in 2 pages should be the same.

How can i do that? Any idea is welcome.

Thank you in advance!
Giang.
0
Comment
Question by:giangdt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 10831386
when you open a new window from an existing all session objects are shared between the windows, so obviously you can not use one session colletion with different items it
what you can do is to create a new session object that is accessable only for the second windo
for example:
in the Page1 create Session["Val1"] and Session["Page1Val2"]
before opening the new window create Session["Page2Val2"] = Session["Page1Val2"]
and in the Page2 modifying the Session["Page2Val2"] object
if you keep in session datatable or something simillar you have to clone it by copy() method

HTH
B..M
0
 

Author Comment

by:giangdt
ID: 10831573
Hi,

Thank you.

But my problem is that the Val2 must is well-known, if not others will not get its value. All pages will submit to a controller, this controller will get the Val2's value to do the thing. So it depends on which page is submited the Controller will get the different value from the Val2.

Any one can help?

Giang.
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 10831603
so create the items like
"pagename" + standartkey

then when you want to get a value from the session collection just get the page that is submitted the data add as suffix the key and you can get the correct value
HTH
B..M
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:giangdt
ID: 10831654
Hi,

So what will happen if there are more than 2 pages (page2) are opened from the page1? They will have the same session item!?

Giang.
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 10831676
you will have Val1 - for page1, page2, page3 ( page2 and page3 are opened from page1 )
and page2Val2 and page3Val2

B..M
0
 

Author Comment

by:giangdt
ID: 10831709

But if i have 2 opened page2s. And each page2 set the different value? What will be happen? The last change of the Val2 will effect the previous change in the another page2.

Giang.
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 10831735
giangdt, there are a lot of ways for searching for what page is currently opened
two of the easiest are:
send this info trhough the query string
create session object "pageID"
open the new window
create a hidden field in your page2 and set it's value to pageID value
dispose Session["pageID"] so if you open another page2 Session["pageID"] will have different value

B..M
0
 

Author Comment

by:giangdt
ID: 10831814
Hi mmarinov,

Can you tell me more details about pageID. What for?
If it will be added to the Val2 key(to make a difference), in this case how can i set the pageID so that this one is always unique? How can Controller know about the session item (pageID is dynamic)?


Thank you,
Giang.
0
 
LVL 28

Accepted Solution

by:
mmarinov earned 50 total points
ID: 10832050
the simple example is to generate GUID
so before open the page2->generate GUID->add created guid to session["PageID"]->open page2->in page2 controller get "PageID" item from the Session object->write somewhere this guid, for example in a hidden field-> dispose session["PageID"]-> Create session[GUID+"page2"]->in page2 work with the new created session

B..M
0
 

Author Comment

by:giangdt
ID: 10905583

Thank you mmarinov

Giang.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question