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
    if(setStoreIt.Equals("KeepItAlive")){
    //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

    Thanx!

  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
    {
        get
            {
                if(myList != null)
                    return (List<int>)ViewState["myList"];
                else
                    return null;
                    // you could also set it to return a new List<int> but I prefer nulls.
            }
            set
            { 
                ViewState["myList"] = value;
            }
    }

    You can store your own objects there as well so long as they are marked Serializeable on the class.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top