0
[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?

2 Answers 2

0

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.

4
  • Thats great, dont understand how I missed that - so all writing to the viewstate should be done on init?
    – Craig
    Commented Aug 9, 2012 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
    Commented Aug 9, 2012 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
    Commented Aug 9, 2012 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
    Commented Aug 9, 2012 at 11:28
0

use this property. this will work.

    [Personalizable(PersonalizationScope.Shared)]
    [WebPartStorage(Storage.Shared)]
    public string CacheF
    {
        get
        {
            if (!string.IsNullOrEmpty((string)ViewState["CacheF"]))
                return (string)ViewState["CacheF"];

            return !string.IsNullOrEmpty(Session["CacheF"]) ? Session["CacheF"] : string.Empty;
        }
        set => ViewState["CacheF"] = Session["CacheF"] = value;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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