Solved

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

Posted on 2004-04-15
10
858 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
  • 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Vb. Net application freezes 9 46
Reading the Web.Config using IIS 7.5? 4 36
VB.Net Report Printing Issue 3 34
JQuery JavaScripot loop though RadioButtonList 4 25
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

773 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