开发者

Passing values across pages ASP.net

开发者 https://www.devze.com 2022-12-25 18:14 出处:网络
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

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().

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号