views:

44

answers:

4

Hi i need to pass a long list of Ids from one page to another, i was thinking of using querystring but taking in consideration that the number of ids to be passed may reach 300 i think is not a good idea. an option from my understanding may be the use of session, this seems to me an optimal solution in my case but on the other hand

I can certainly clear the session when the user performs a certain action and the list of ids is not longer needed, but if the user leaves the page without performing any action i want to clear the session, how can i do it?

If multiple users are using the same functionality do i need the session's items to be confused or the session is for a single user?

I am working in asp.net c#

Thanks

A: 

Session is per 'user' (more like browser instance).

Session expires after some preset/predefined timeout.

leppie
A: 

Sessions work per user, as long as you are not load balancing a web server you could do this using sessions (maybe a dataset or hashtable session variable, this could get ugly...).

To clear a session look into: Session.Contents.Remove.

JonH
You could still use sessions even if load balancing, so long as you stored them in state server or SQL server (rather than in process).
Dan Diplo
@Dan - I was more on the lines of mentioning it based on a single web server. If multiple that of course has to be done with sql /state server.
JonH
+2  A: 

If multiple users are using the same functionality and the id's are the same for each user, then use Application state, or a Singleton pattern.

If each user needs to have their own list of id's then you can use Session state which is unique for each user. In classic asp you shouldn't use an object eg collection but in ASP.NET you could place these in a list, array etc.

In ASP.NET you can share session state between servers if you enable the session service, or place session state in a database.

You could also consider cookies if the id's are numeric you could comma separate them and reload them with relatively low overhead, but with the caveat that a smart user may be able to alter the values.

Finally, you could store these values in a database table between calls.

James Westgate
Yes but how do I know when the user leaves the page and i need to clear the session?
GigaPr
Normally the pattern is to wait for the session to time out, it is then cleared automatically. Otherwise you can clear it when a user eg clicks on a link / button during a postback.
James Westgate
A: 

Use a combination of the two methods - query string and Session.

When you need to store your IDs, whack them into Session, but the key you use should be a randomly generated Guid:

string key = Guid.NewGuid().ToString();
Session[key] = myIDs;

Remember that guid, and append it to the query string when the user leaves the page (they will be clicking on a hyperlink or clicking a button to leave the page, you can append it then).

If the user doesn't perform the magic action and leaves the page, the key to retrieve the IDs gets lost when they leave. The IDs that were placed in to Session will be removed when the Session recycles/terminates, or when you do a Session.Clear().

slugster