Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.
[Personalizable(PersonalizationScope.Shared)]
    [WebPartStorage(Storage.Shared)]
        public List<searchResult> lsQtys
    {
      get
      {
          List<searchResult> lsQuantities = (List<searchResult>)ViewState["lsQuantities"];
          return lsQuantities;
      }
        set
        {
            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
add comment

1 Answer 1

up vote 0 down vote accepted

I think you want to take a look at this http://msdn.microsoft.com/en-us/library/ms178472.aspx

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 msdn.microsoft.com/en-us/library/… and also this one msdn.microsoft.com/EN-US/library/ae8kbty0 hope the links help. –  Robban1980 Aug 9 '12 at 11:28
add comment

Your Answer

 
discard

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.