Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-04-15
10
Medium Priority
?
879 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
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 

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 150 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Integration Management Part 2
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

783 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