Passing values across pages ASP.net
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 o开发者_开发百科ther 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
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.
Session is per 'user' (more like browser instance).
Session expires after some preset/predefined timeout.
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.
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()
.
精彩评论