Solved

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

Posted on 2004-04-15
10
852 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:giangdt
Comment Utility
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
Comment Utility
you will have Val1 - for page1, page2, page3 ( page2 and page3 are opened from page1 )
and page2Val2 and page3Val2

B..M
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:giangdt
Comment Utility

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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility

Thank you mmarinov

Giang.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now