SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
        public List<searchResult> lsQtys
          List<searchResult> lsQuantities = (List<searchResult>)ViewState["lsQuantities"];
          return lsQuantities;
            ViewState["lsQuantities"] = value;

I build up this list of varaibles when the webpart is first rendered and using the immediate window i can see that the viewstate is storing the variables. However on the button press the information stored in the viewstate is then null? is there any reason why this information would be lost/unavailable on postback?

share|improve this question
up vote 0 down vote accepted

I think you want to take a look at this

Explaining the ASP.Net lifecycle of a page. And here you can find where the ViewState values are added to the Viewstate collection on a postback.

share|improve this answer
Thats great, dont understand how I missed that - so all writing to the viewstate should be done on init? – Craig Aug 9 '12 at 10:04
Depends on what you want to do. If you want to work with your postback data it should be restored after the Init event and be accessible at PreLoad or Load. But if it for example want to work with a dropdownlist that has dynamically added data, then you need to load all values in the list before the ViewState tries to select what you selected in the control. Sorry if i'm unclear was a while since i worked with Viewstate and ASP.Net Forms. – Robban1980 Aug 9 '12 at 10:13
I am wanting to store a list of a custom datatype in the viewstate. At what point should these be written? I have read initComplete but this does not run(action listner declared in onInit) – Craig Aug 9 '12 at 10:44
I think you want to populate your viewstate on the Load event. As at this stage the previous viewstate has been restored if any. i think you also want to take a look at this… and also this one hope the links help. – Robban1980 Aug 9 '12 at 11:28

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.