ASP.NET webforms maintain List across postbacks

Go To StackoverFlow.com

0

Hello I have a small List<string> that I want to maintain across postbacks on only ONE page. The List will not contain more than 10 items, with each item being 40 chars max (no sensitive data).

I know that similar questions have been asked, but I am somewhat ambivalent between storing this in ViewState or Session. I've heard that even small string values can cause the ViewState value to increase quite largely in size. I would like to iterate through the List and use Linq to query it, but would I be better off using a string[] array, and would this List (of humble size) be okay to store in ViewState or should I be storing it in Session? Thank you.

2012-04-03 22:49
by maGz


1

Storing things in ViewState isn't a terrible idea if you don't abuse it. If the list is small and it only makes sense on one page, then go for it.

Just remember ViewState can be tampered with, so if you don't want the client to know about it, then you are golden. Otherwise store it in Session.

2012-04-03 22:57
by Josh
Hey Josh, thanks for that. Is there any compelling reason to store it in Session - maGz 2012-04-03 23:04
This probably is only one of 2 things in my entire application that I'm purposefully wanting to store in ViewState - maGz 2012-04-03 23:05
@maGz - Only if you want it to persist across the entire session, or have access to it in different pages. I tend to avoid using session as much as possible simply because it can make scaling more difficult if you are heavily dependent on session. Like for instance getting bounced to a different server in a web farm. If it only exists for a single page, then ViewState is your best bet. Again, just don't abuse it. Large ViewState is it's own form of evil - Josh 2012-04-03 23:59
thanks Josh! good advic - maGz 2012-04-04 00:20


2

I would be more concerned about the scope of this List<>. If it's limited only to this single page, then I think it is perfectly acceptable to use the ViewState; that's exactly what it was meant for.

However if you need to persist this across multiple pages, then it would make more sense to evaluate Session or Cache.

I would also keep in mind if the list is different for each user, or if it could be shared amongst multiple users. In that case I would lean towards Cache.

Finally, and I'll probably be downvoted into oblivion on this one, but you could also look at the option of storing the ViewState in the server-side memory using the SessionPageStatePersister class.

Good luck!

2012-04-03 23:16
by msigman
all info helps in my learning so I'll +1 to u! I don't need it persisted across multiple pages (in fact I'd prefer if the data wasn't available anywhere else, although I am clearing all Session vars OnLoggedOut using Session.Abandon()). It will change poer user, as I basically want to keep track of the filenames of photos uploaded by users...the thing is that users can also choose to omit certain photos that they have already uploaded, and also provide me a way to check if they are not uploading more than 10 photos - maGz 2012-04-03 23:24