Automatic Methods don't hold their values after postback

2 posts, 1 answers
  1. Sintayehu
    Sintayehu avatar
    67 posts
    Member since:
    Jan 2013

    Posted 04 Mar 2009 Link to this post

    //page load


    protected void Page_Load(object sender, EventArgs e)



    // check if StoreIt is not empty
    //Do something

    public string storeIt{get; set}

    private void setStoreIt()
        StoreIt = "KeepItAlive";

    Unfortunately the setStoreIt Automatic Method gets reset everytime a postBack happenes.

    I always used HiddenFields to get around this issue.

    But today I am presented with an issue where i have to keep a <List> alive.

    Any ideas?

    Is this a problem with ajaxified pages?

    Can I add the automatic method in the aJaxManajer? Ha Ha


  2. Answer
    Mark Fitzpatrick
    Mark Fitzpatrick avatar
    36 posts
    Member since:
    Jan 2004

    Posted 04 Mar 2009 Link to this post

    They won't because you're not storing them anywhere. Once the page is done processing the local variables are destroyed. You have to store the data somewhere in order to retrieve it across postbacks. ASP.Net does provide a mechanism around this through the ViewState. Let's take a generic list of integer values

    public List<int> myList
                if(myList != null)
                    return (List<int>)ViewState["myList"];
                    return null;
                    // you could also set it to return a new List<int> but I prefer nulls.
                ViewState["myList"] = value;

    You can store your own objects there as well so long as they are marked Serializeable on the class.
Back to Top